Solved

Worpdress WP_Query exclude children

Posted on 2011-02-17
8
1,606 Views
Last Modified: 2012-06-22
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
Comment
Question by:m2ew
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 23

Accepted Solution

by:
jeremyjared74 earned 300 total points
ID: 34923319
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
 

Author Comment

by:m2ew
ID: 34923563
How do I make this work for custom taxonomy that is hierarchical?
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 34927635
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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

by:m2ew
ID: 34927684
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
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 34927709
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
 

Author Comment

by:m2ew
ID: 34927731
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
 

Author Comment

by:m2ew
ID: 34928055
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
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 34928451
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question