WordPress.org

Support

Support » How-To and Troubleshooting » WP query not working for categories

WP query not working for categories

  • Hello,
    i am using the following code in which i exclude specific categories.

    $recent = array(
      'posts_per_page' => 10,
      'cat=-1,-12,-13,-14,-15,-16,-17,-20,-195,-245,-203'
    );
    
    $recentPosts = new WP_Query($recent); ?>
    		<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    //code for showing the posts
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>

    The number of shown posts (10) work fine, i cant make it work the exclusion of the categories…

    Its seems really strange. Any sollutions??
    I have used the code from here: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    Thanks in advance
    Kostas

Viewing 10 replies - 1 through 10 (of 10 total)
  • If to read the Codex page from your link attentively:
    'category__not_in' => array( 2, 6 )

    I think it works!
    Thanks vjpo!

    I have another question thow. I have many categories, and many subcategories in each category. I have inserted in the code above the main category i want to exclude, but it stil shows the posts of the subcategories of this category.

    Is there anything i can do in order not to insert all the subcategories by hand and only add the ID of the parent category???

    Thanks again!

    There is a number of different solutions how to exclude automatically child categories from a query, but it’s impossible to do by using only WP_Query parameters. What solution to use – it depends from the task.
    You can try also to use in WP_Query 'category__in'instead of 'category__not_in'.

    Yes this is one sollution.
    But if i create a new parent category, i will probably have to edit the code and add the categorys’ ID in the ‘category_in’ function.

    Automatic solution is possible, but where do you place this code (single.php, archive.php) what is the final task? I mean, are this categories related to the context they must be displayed?

    I am placing it in a custom page template. page-10.php. In this page i am displaying the 10 newest posts and some text at the top of it.

    So, the context is static – always in page. In this situation this is maximal automation that came into my head. Almost nothing ))) It has any meaning if there is a lot of child categories.

    $exclude = array (1, 12, 13, 14);
    $child_categories=  get_categories('child_of=15');
    // add child of cat 15 to excluded categories
    foreach ($child_categories as $category) {
        $exclude[] = $category->term_id;
    }
    $recent = array( 'posts_per_page' => 10, 'category__not_in' => $exclude );

    Moderator Michael

    @alchymyth

    Forum Moderator

    just an idea: try to loop through all to be excluded categories to catch all possible child cats:

    $exclude = array (1, 12, 13, 14);
    foreach( $exclude as $ex_cat ) {
      $child_categories=  get_categories('child_of='.$ex_cat);
      // add all childs of exclude cats to excluded categories
      if( $child_categories ) foreach ($child_categories as $category) {
        $exclude[] = $category->term_id;
      }
    }
    $recent = array( 'posts_per_page' => 10, 'category__not_in' => $exclude );

    (not tested)

    Thanks, alchymyth! Nice improvement! Just tested – it works )

    You guys rock!!!

    Thank you very much!!!!!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘WP query not working for categories’ is closed to new replies.