WordPress.org

Ready to get started?Download WordPress

Forums

WP query not working for categories (11 posts)

  1. little_kostas
    Member
    Posted 2 years ago #

    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

  2. vjpo
    Member
    Posted 2 years ago #

    If to read the Codex page from your link attentively:
    'category__not_in' => array( 2, 6 )

  3. little_kostas
    Member
    Posted 2 years ago #

    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!

  4. vjpo
    Member
    Posted 2 years ago #

    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'.

  5. little_kostas
    Member
    Posted 2 years ago #

    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.

  6. vjpo
    Member
    Posted 2 years ago #

    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?

  7. little_kostas
    Member
    Posted 2 years ago #

    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.

  8. vjpo
    Member
    Posted 2 years ago #

    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 );
  9. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    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)

  10. vjpo
    Member
    Posted 2 years ago #

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

  11. little_kostas
    Member
    Posted 2 years ago #

    You guys rock!!!

    Thank you very much!!!!!

Topic Closed

This topic has been closed to new replies.

About this Topic