[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1667
  • Last Modified:

Worpdress WP_Query exclude children

Is there a way to query posts and exclude any posts that are in subcategories (ie. only in main category)?

I have tried array('category__in' => array($mycatID)) in my query_posts argument, but it is still pulling children category posts.
0
m2ew
Asked:
m2ew
  • 4
  • 4
1 Solution
 
jeremyjared74Commented:
Try using this snippet:

<?php $category_ID = $cat; // get ID of current category ?>

<?php $excludes = get_categories('child_of='.$category_ID) ;


    foreach ( $excludes as $key => $value ){
        $exs[] = $value->cat_ID;
    }

$my_query = new WP_Query(array(
            'cat' => $category_ID,
            'category__not_in' => $exs

));
if ($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post();
?>

Open in new window

0
 
m2ewAuthor Commented:
How do I make this work for custom taxonomy that is hierarchical?
0
 
jeremyjared74Commented:
The question wasn't, but the answer was accurate. I'm sorry I couldn't help with the modified question, but that doesn't change the fact that I spent time trying to assist with the actual question.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
m2ewAuthor Commented:
True. Thanks for you time. I have assigned the points to you and posted new question @ http://www.experts-exchange.com/Web_Development/Blogs/WordPress/Q_26831743.html
0
 
jeremyjared74Commented:
I will look some more for your answer. I tried last night to find a solution, but didn't have any luck. That is a tricky one. I have seen all the listed array options for taxonomies, and excluding child pages weren't one of them. There is always a work-around though. If I find it I will post it for you.
0
 
m2ewAuthor Commented:
Thanks. I did look at this http://stackoverflow.com/questions/3449815/wordpress-how-can-i-exclude-posts-in-child-taxonomies-from-a-custom-taxonomy-que

Just trying to figure out how to merge with current args.
0
 
m2ewAuthor Commented:
So it turns out that was right. I ran

$unwanted_children = get_term_children($category->term_id, $category->taxonomy);
                                //print_r($unwanted_children);
$unwanted_post_ids = get_objects_in_term($unwanted_children, $category->taxonomy);
                              //print_r($unwanted_post_ids);
                              echo sizeof($unwanted_post_ids);

then I just added in 'post__not_in' => $unwanted_post_ids into my query_posts array and it worked like charm.
0
 
jeremyjared74Commented:
Good, I'm glad you found a solution. Sometimes the info is out there, just hard to find or needs a little tweaking. This will be a good question to add to the Experts Exchange Knowledge-base.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now