• I am trying to exclude a category from some loop and also from search results, i am using the following args array

    $args = array(
    	'posts_per_page' => '15',
    	'cat' => $cat,
    	'paged' => $paged,
    	'category__not_in' => '1164',
    	'order' => $order
    );

    this seems to work in normal loops, the category 1164 is removed from posts. But when the same used on search, posts in category 1164 still shows through.

    please help, thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • please review http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    'category__not_in' requires an array as argument.

    also review and possibly adapt http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Exclude_Pages_from_Search_Results

    what theme are you using?

    what is the full code of search.php?

    Thread Starter bluerainsky

    (@bluerainsky)

    I have tried using array for category__not_in parameter, still the same results, category 1164 posts still shows up.

    i am using custom theme. The search.php just has basic query and then loop through the query to display each post. I am using the exact same code for search and for category. The category__not_in parameter works in category, and 1164 is removed. But in search, it doesn’t work.

    $paged = get_query_var( 'paged' );
    $cat = get_query_var( 'cat' );
    $term = get_query_var('s');
    $order = 'DESC';
    
    $args = array(
    	'posts_per_page' => '15',
    	'cat' => $cat,
    	'paged' => $paged,
    	'category__not_in' => array('1164'),
    	'order' => $order
    );
    
    $loop = new WP_Query($args);

    You don’t say. I recently have exact same problem on one of my client’s website using one of WooTheme’s theme (Spectrum News).

    No matter how I debug, the ‘category__not_in’ just not working. I fixed it like this:

    while (in_array($post->ID, $exclude) ) the_post();

    Definitely want to know what is the real cause and proper fix.

    it seems that there are some mistakes in your query:
    – you are not adding the search parameter – http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter;
    – you are using a cat parameter based on get_query_var('cat') which is not defined in the serach template.

    forget the custom query, and use the ‘normal’ loop;
    then, based on http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Exclude_Pages_from_Search_Results, try adding this to functiosn.php of your theme:

    function search_filter($query) {
      if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_search) {
          $query->set('category__not_in', array( 1 ) );
        }
      }
    }
    
    add_action('pre_get_posts','search_filter');

    if you need to persist with your (existing) search.php template, please post the full code for further suggestions.

    Thread Starter bluerainsky

    (@bluerainsky)

    thanks for helping!

    below is the code that i am using. i also have what you suggested ‘search_filter’ function in my function.php file. However, it’s still not working. can you please suggest how i can fix this problem?

    function search_filter($query) {
      if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_search) {
          $query->set('category__not_in', array( 1164 ) );
        }
      }
    }
    add_action('pre_get_posts','search_filter');
    <?php
    	global $wp_query, $query_string;
    	$paged = get_query_var('paged');
    	$term = get_query_var('s');
    	$args = array (
    		'posts_per_page' => '15',
    		'paged' => $paged,
    		'order' => 'DESC'
    	);
    	query_posts($args);
    	$numpages = $wp_query->max_num_pages;
    	$numposts = $wp_query->found_posts;
    ?>
    
    	<?php if( have_posts() ) : ?>
        	<?php while( have_posts() ) : the_post(); ?>
                    <a>">
                        <span><?php the_title(); ?></span>
                    </a>
    	<?php endwhile; ?>
    	<?php endif; ?>
        <?php wp_reset_query(); ?>
    
    <?php if ( $numpages > 1 && $paged < $numpages) : ?>
        <?php echo next_posts_link( '  »   '); ?>
    <?php endif; ?>
    <?php if ( $paged > 1 ) : ?>
        <?php echo previous_posts_link( '  «   ' ); ?>
    <?php endif; ?>

    This is my category.php. I want to filter some posts from this category. Posts were posted in parent and child categories. Show only posts from this category and not from child categries. “category__not_in” not working when argument given as variable. If I use ‘category__not_in’=> array(20,22,23,24) like this it works perfectly.. In below code ‘category__not_in’=> array($sub_cat_ids) not works. please help

    <?php
    $current_cat_id = get_cat_id( single_cat_title("",false) );
    $catlist = get_categories('hide_empty=0&child_of='.$current_cat_id.'&depth=10&parent='.$current_cat_id );
    $sub_cat_ids = "";
    foreach($catlist as $cat){
       $sub_cat_ids = $cat->cat_ID.",".$sub_cat_ids;
    }
    if(!empty($catlist)){ ?>
        <ul class="insight_style">
        <?php
            $i=1;
            $args = array(
            'post_type' => 'post',
            'posts_per_page' => -1,
            'category__not_in'=> array($sub_cat_ids),// not working
            'category__in' => $current_cat_id,
            'paged' => $paged
             );
            $page_query = new WP_Query($args);
            if ( $page_query->have_posts() ) : while ($page_query->have_posts()) : $page_query->the_post(); 
    
                } ?>
                <li>
                // do some stuff
                </li>
                <?php  $i++; endwhile; endif; wp_reset_query(); ?>
            </ul>
        <?php  } ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘category__not_in doesn't work in search’ is closed to new replies.