Wordpress:使用post__not_in排除自定义分类法类别

时间:2017-03-02 19:23:54

标签: php wordpress

我试图从循环中排除自定义分类法类别(slug:private-case-study,ID:5)中的项目。我使用以下代码,是否有人有任何建议或想法,为什么它不起作用?提前谢谢!


    // Get the current page ID
        $this_post = $post->ID;
        $private_case_study = get_term_by('slug', 'private-case-study', 'mgt_portfolio_filter');


        // Show items from specific category
        if($category_name == '') {
            $wp_query = new WP_Query(array(
                'post_type' => 'mgt_portfolio',
                'posts_per_page' => $posts_per_page,
                'orderby'    => $orderby,
                'order' => $order,
                'post__not_in' => array($this_post, $private_case_study)
            ));
        } else {
            $wp_query = new WP_Query(array(
                'post_type' => 'mgt_portfolio',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'mgt_portfolio_filter',
                        'field'    => 'slug',
                        'terms'    => $category_name,
                    ),
                ),
                'posts_per_page' => $posts_per_page,
                'orderby'    => $orderby,
                'post__not_in' => array($this_post, $private_case_study),
                'order' => $order
            ));
        }

2 个答案:

答案 0 :(得分:2)

//获取当前页面ID         $ this_post = $ post-> ID;         $ private_case_study = get_term_by(' slug','私人案例研究',' mgt_portfolio_filter');

    // Show items from specific category
    if($category_name == '') {
        $wp_query = new WP_Query(array(
            'post_type' => 'mgt_portfolio',
            'posts_per_page' => $posts_per_page,
            'orderby'    => $orderby,
            'order' => $order,
            'post__not_in' => array($this_post)
        ));
    } else {
        $wp_query = new WP_Query(array(
            'post_type' => 'mgt_portfolio',
            'tax_query' => array(
                array(
                    'taxonomy' => 'mgt_portfolio_filter',
                    'field'    => 'slug',
                    'terms'    => $category_name,
                ),
            ),
            'posts_per_page' => $posts_per_page,
            'orderby'    => $orderby,
            'post__not_in' => array($this_post),
            'order' => $order
        ));
    }

答案 1 :(得分:0)

post__not_in获取一系列ID并将其排除。您希望使用category__not_in,它会获取一系列类别ID并将其排除。

查看documentation