按分类和元值过滤帖子

时间:2015-08-12 作者:Chris

我正在编写一个插件,它将根据标签和Posteta对帖子进行隐藏和排序。默认情况下,标记为“古董”的缺货产品将被隐藏。到目前为止,我可以隐藏已售出的产品,但当我试图隐藏标记为“古董”的已售出产品时,我的代码失败了。我希望有人能帮我找到解决办法。我想我在WHERE子句或terms表的左JOIN语句中遇到了问题。这是我的代码:

add_filter( \'posts_join\', \'meta_join\' );

function meta_join( $join ) {
    if ( ( is_woocommerce() || is_search() ) && ! is_admin() ) {
        global $wpdb;
        $add = \'\';
        if ( ! is_product_category() && ! is_product_tag() ) {
            $add = " LEFT JOIN $wpdb->term_relationships ON ( $wpdb->posts.ID = $wpdb->term_relationships.object_id ) ";
        }
        $add .= " 
            LEFT JOIN $wpdb->postmeta AS stock ON ( 
                $wpdb->posts.ID = stock.post_id 
                AND stock.meta_key = \'_stock_status\' 
            ) 
            LEFT JOIN $wpdb->term_taxonomy wtt ON ( 
                $wpdb->term_relationships.term_taxonomy_id = wtt.term_taxonomy_id 
                AND wtt.taxonomy = \'product_tag\'
            ) 
            LEFT JOIN $wpdb->terms wt ON ( wtt.term_id = wt.term_id )
        ";
        return $join . $add;
    }
    return $join;
}

add_filter(\'posts_where\', \'hide_sold_antiques\');

function hide_sold_antiques( $where ) {
    if ( ! show_sold_antiques() && ! is_product() && ! is_search() && is_woocommerce() && ! is_admin() ) {
        global $wpdb;
        $add = " AND NOT ( 
            stock.meta_value = \'outofstock\' 
            AND wt.slug = \'antique\'
        ) ";
        return $where . $add;
    }
    return $where;
}

add_filter( \'posts_orderby\', \'product_meta_orderby\' );

function product_meta_orderby( $orderby ) {
    if ( ( is_woocommerce() || is_search() ) && ! is_admin() ) {
        global $wpdb;
        return " $wpdb->posts.post_type DESC, stock.meta_value ASC, $wpdb->posts.post_date DESC ";
    }
    return $orderby;
}

function show_sold_antiques() {
    $show_sold_antiques = false;
    $showsold_parameter = null;
    if ( isset( $_GET[ \'showsold\' ] ) ) {
        $showsold_parameter = $_GET[ \'showsold\' ];
        if ( $showsold_parameter === \'true\' ) {
            $show_sold_antiques = true;
        }
    }
    return $show_sold_antiques;
}

1 个回复
最合适的回答,由SO网友:Chris 整理而成

我终于能够在来自my more MySQL oriented question 堆栈溢出时:

add_filter( \'posts_fields\', \'posts_fields\' );

function posts_fields( $fields ) {
    if ( ( is_woocommerce() || is_search() ) && ! is_admin() ) {
        $add = "
            , CASE wt.slug 
            WHEN \'antique\' THEN 1 
            WHEN \'reproduction\' THEN 2 
            ELSE 0 END 
            as slug_order
        ";
        return $fields . $add;
    }
    return $fields;
}

add_filter( \'posts_join\', \'meta_join\' );

function meta_join( $join ) {
    if ( ( is_woocommerce() || is_search() ) && ! is_admin() ) {
        global $wpdb;
        $add = \'\';
        $add = " 
            LEFT JOIN $wpdb->postmeta stock ON ( 
                $wpdb->posts.ID = stock.post_id 
                AND stock.meta_key = \'_stock_status\' 
            ) 
            LEFT JOIN $wpdb->term_relationships wtr ON ( 
                $wpdb->posts.ID = wtr.object_id 
                AND stock.meta_value = \'outofstock\'
            )
            LEFT JOIN $wpdb->term_taxonomy wtt ON ( 
                wtr.term_taxonomy_id = wtt.term_taxonomy_id 
                AND wtt.taxonomy = \'product_tag\'
            ) 
            LEFT JOIN $wpdb->terms wt ON (
                wtt.term_id = wt.term_id 
                AND wt.slug IN( \'antique\',\'reproduction\' ) 
            )
        ";
        return $join . $add;
    }
    return $join;
}

add_filter(\'posts_where\', \'hide_sold_antiques\');

function hide_sold_antiques( $where ) {
    if ( ! is_product() && ! is_search() && is_woocommerce() && ! is_admin() ) {
        $add = \'\';
        if ( ! show_sold_antiques() ) {
            global $wpdb;
            $add = " AND NOT ( 
                stock.meta_value = \'outofstock\' 
                AND $wpdb->posts.ID IN (
                    SELECT object_id 
                    FROM $wpdb->term_relationships wtr
                    INNER JOIN $wpdb->term_taxonomy wtt ON ( 
                        wtr.term_taxonomy_id = wtt.term_taxonomy_id 
                        AND wtt.taxonomy = \'product_tag\'
                    ) 
                    INNER JOIN $wpdb->terms wt ON ( 
                        wtt.term_id = wt.term_id 
                        AND wt.slug = \'antique\'
                    )
                )
            ) ";
        }
        $add .= " AND NOT ( 
            stock.meta_value = \'outofstock\' 
            AND (
                CASE wt.slug 
                WHEN \'antique\' THEN 1 
                WHEN \'reproduction\' THEN 2 
                ELSE 0 END
            ) = 0
        ) ";
        return $where . $add;
    }
    return $where;
}

add_filter( \'posts_groupby\', \'product_groupby\' );

function product_groupby( $groupby ) {
    if ( ( is_woocommerce() || is_search() ) && ! is_admin() ) {
        return $groupby . \', slug_order\';
    } else {
        return $groupby;
    }
}

add_filter( \'posts_orderby\', \'product_meta_orderby\' );

function product_meta_orderby( $orderby ) {
    if ( ( is_woocommerce() || is_search() ) && ! is_admin() ) {
        global $wpdb;
        return " $wpdb->posts.post_type DESC, stock.meta_value ASC, slug_order DESC, $wpdb->posts.post_date DESC ";
    }
    return $orderby;
}

function show_sold_antiques() {
    $show_sold_antiques = false;
    $showsold_parameter = null;
    if ( isset( $_GET[ \'showsold\' ] ) ) {
        $showsold_parameter = $_GET[ \'showsold\' ];
        if ( $showsold_parameter === \'true\' ) {
            $show_sold_antiques = true;
        }
    }
    return $show_sold_antiques;
}

结束

相关推荐

Custom taxonomy page template

我第一次在写我自己的wordpress主题。我用自定义分类法注册了一个新的帖子类型,但我无法按术语显示帖子。我复制了档案。php并将其重命名为taxonomy-[mycustomtaxonomy]。php并修改了几行。我保留了档案中的循环。php:<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php the_content(); ?> <?php