简体   繁体   English

wordpress - category__not_in 不工作

[英]wordpress - category__not_in not working

I'm having a problem getting my query function.我在获取查询功能时遇到问题。 I need to run the loop, excluding a particular category.我需要运行循环,不包括特定类别。

I'm trying to use category__not_in , but is not working at all some.我正在尝试使用category__not_in ,但有些根本不起作用。

<?php
  $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category__not_in' => array( '44' ),
    'posts_per_page' => 9,
    'paged' => get_query_var('paged')
  );
  $query = new WP_Query( $args );

  query_posts($query);
?>

I've already tried:我已经尝试过:

'category__not_in' => array( '44' ),
'category__not_in' => array( 44 ),
'category__not_in' => '44',
'category__not_in' => 44,

But nothing works =(但没有任何效果=(

Try using tax_query instead :尝试使用 tax_query 代替:

<?php
  $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 9,
    'paged' => get_query_var('paged'),
    'tax_query' => array(
        array(
            'taxonomy' => '<YOUR TAXONOMY NAME>',
            'field'    => 'term_id',
            'terms'    => array( 44 ),
            'operator' => 'NOT IN',
        ),
    ),

  );
  $query = new WP_Query( $args );

  query_posts($query);
?>

Use 'cat' => '-44' in your $args array:$args数组中使用'cat' => '-44'

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'cat' => '-44',
    'posts_per_page' => 9,
    'paged' => get_query_var('paged')
);

It's the way recommended in the WP Codex .这是WP Codex中推荐的方式。

Thanks guys, it worked thanks to @rnevius谢谢大家,感谢@rnevius

The problem was in my query, I was using WP_Query() and query_posts() .问题出在我的查询中,我使用的是WP_Query()query_posts()

I used how reference the WP Codex: https://codex.wordpress.org/Class_Reference/WP_Query我使用了如何参考 WP Codex: https ://codex.wordpress.org/Class_Reference/WP_Query

Below is how my code was at the end:下面是我的代码最后的样子:

<?php
  $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category__not_in' => array( 44 ),
    'posts_per_page' => 9,
    'paged' => get_query_var('paged')
  );
  $query = new WP_Query( $args );
?>

<?php
  if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
      $query->the_post();
?>

// code

<?php
    }
  } else {
    // no posts found
  }
  wp_reset_postdata();
?>

To exclude a category in the search use this:要在搜索中排除类别,请使用以下命令:

function search_filter($query)
{
if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search)
    {
        $taxquery = array(
            array(
                'taxonomy'  => 'category',
                'field'     => 'term_taxonomy_id',
                'terms'     => 244,
                'operator'  => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $taxquery );
    }
}

} }

add_action('pre_get_posts','search_filter'); add_action('pre_get_posts','search_filter');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM