如何从类别数组中选择帖子而不在其他类别的数组中选择?

时间:2015-03-04 12:16:11

标签: php mysql sql wordpress

这是我想要实现的目标:

我的帖子属于A,B,C,D类。我想要显示A类中包含的帖子,但我不想显示C类中包含的帖子。所以,如果我运行查询,我希望此帖子在A,B,C,D中不显示,因为它也属于C类。我怎么做到这一点?

  $notInCategories = array(103,106,132,102,10,57,58,28,48);    
    $args = array(
                            'post_type' => 'post',
                            'post_status' => 'publish',
                            'order_by' => 'ID',
                            'tax_query' => array(
                            'relation' => 'AND',
                                    array(
                                            'taxonomy' => 'category',
                                            'field'    => 'id',
                                            'terms'    => array('10','11','12')
                                    ),
                                    array(
                                            'taxonomy' => 'category',
                                            'field'    => 'id',
                                            'terms'    => $notInCategories,
                                            'operator' => 'NOT IN',
                                    ),
                            ),
                            'meta_query' => array( 
                                'relation' => 'AND',
                                        array(
                                         'key' => 'joke_type',                 
                                         'value' => $type,                 
                                         'type' => 'CHAR',                 
                                         'compare' =>  '='
                                     ),
                                         array(
                                             'key' => 'joke_rating',
                                             'value' => 3,
                                             'type' => 'SIGNED',
                                             'compare' => '>='
                                         )
                             ) 
                    );
                    $myquery = new WP_Query( $args );
                    echo '<p>REQUEST:'.$myquery->request.'</p>';

这是打印的内容:

    REQUEST:SELECT SQL_CALC_FOUND_ROWS wp_njkf_posts.ID FROM wp_njkf_posts 
INNER JOIN wp_njkf_term_relationships ON (wp_njkf_posts.ID = wp_njkf_term_relationships.object_id) 
INNER JOIN wp_njkf_postmeta ON ( wp_njkf_posts.ID = wp_njkf_postmeta.post_id ) 
INNER JOIN wp_njkf_postmeta AS mt1 ON ( wp_njkf_posts.ID = mt1.post_id ) 
WHERE 1=1 AND ( wp_njkf_term_relationships.term_taxonomy_id IN (11,12,13) 
AND wp_njkf_posts.ID NOT IN ( SELECT object_id FROM wp_njkf_term_relationships 
WHERE term_taxonomy_id IN (11,30,50,59,60,104,105,108,134) ) ) 
AND wp_njkf_posts.post_type = 'post' 
AND ((wp_njkf_posts.post_status = 'publish')) 
AND ( ( wp_njkf_postmeta.meta_key = 'joke_type' 
AND CAST(wp_njkf_postmeta.meta_value AS CHAR) = 'joke' ) 
AND ( mt1.meta_key = 'joke_rating' AND CAST(mt1.meta_value AS SIGNED) >= '3' ) ) 
GROUP BY wp_njkf_posts.ID 
ORDER BY wp_njkf_posts.post_date
DESC LIMIT 0, 10

LATER EDIT:我现在明白为什么它为term_taxonomy_id打印的方式不同。这是因为term_taxonomy_id与类别ID不同。 term_taxonomy_id是类别id(表term_taxonomy中的term_id)和post(term_relationship中的object_id)之间的连接 - 我不明白它为什么不排除那些类别..

5 个答案:

答案 0 :(得分:0)

尝试在php文件中使用以下代码。

$args = array(
    'post_type' => 'post',
    'posts_per_page'=> your_desired_number_of_post,
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'your_category_here' ),
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'your_category_to_exclude_here' ),
            'operator' => 'NOT IN',
        ),
    ),
);
$query = new WP_Query( $args );

我希望有所帮助,

MMK。

答案 1 :(得分:0)

您可以使用group byhaving执行所需操作。将复杂的查询与简单的问题联系起来有点困难。但是,假设带有id列表的表是wp_njkf_term_relationships,那么你需要类似的东西:

select tr.object_id
from wp_njkf_term_relationships tr
group by tr.object_id
having sum(tr.term_taxonomy_id in (139, 133, 45, 135) ) > 0 and
       sum(tr.term_taxonomy_id in (103, 106, 132, 102, 10, 57, 58, 28, 48) ) = 0;

我建议您一次添加一个表的其余逻辑。

答案 2 :(得分:0)

您如何看待使用子查询:

SELECT * FROM (SELECT * FROM table WHERE category IN ('A') ) AS posts_category_a WHERE category NOT IN ('C') 

答案 3 :(得分:0)

$args = array(
    'post_type' => 'post',
    'posts_per_page'=> 100,
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'CAT 1' ),
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'CAT 2','CAT 3', 'CAT 4' ),
            'operator' => 'NOT IN',
        ),
    ),
);

我有4个帖子:post1,post1.1,post2和post3

所有帖子都在CAT 1,CAT 2和CAT 3类别中。

post1.1仅属于CAT 1类。

当我运行代码时,结果只包含一个帖子,该帖子是post1.1。

我希望这会对你有所帮助。

答案 4 :(得分:0)

当我必须检查它们不属于这些特定类别时,我最终使用了这个:

$args = array(
                            'orderby' =>'rand',
                        'post_type' => 'post',
                        'post_status' => 'publish',
                        'posts_per_page' => 10,
                        'order_by' => 'ID',
                        'category__not_in' => $notInCategories,
                        'meta_query' => array( 
                                    'relation' => 'AND',
                                    array(
                                     'key' => 'joke_type',                 
                                     'value' => $type,                 
                                     'type' => 'CHAR',                 
                                     'compare' =>  '='
                                 ),
                                     array(
                                         'key' => 'joke_rating',
                                         'value' => 3,
                                         'type' => 'SIGNED',
                                         'compare' => '>='
                                     )
                         ),
                         'fields' => 'ids' 
                );

这是因为他们需要处于特定类别时:

$args = array(
                        'orderby' =>'rand',
                        'post_type' => 'post',
                        'post_status' => 'publish',
                        'posts_per_page' => 10,
                        'order_by' => 'ID',
                        'category__in' => $categories,
                        'meta_query' => array( 
                                    'relation' => 'AND',
                                    array(
                                     'key' => 'joke_type',                 
                                     'value' => $type,                 
                                     'type' => 'CHAR',                 
                                     'compare' =>  '='
                                 ),
                                     array(
                                         'key' => 'joke_rating',
                                         'value' => 3,
                                         'type' => 'SIGNED',
                                         'compare' => '>='
                                     )
                         ),
                         'fields' => 'ids' 
                );

它有更多的代码,但最后还有一个如果做了诀窍......我本来想理解为什么我以前的尝试没有用,但我想这仍然存在于Wordpress的奥秘中。 / p>