Solved

Worpdress WP_Query exclude children

Posted on 2011-02-17
8
1,559 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
WordPress can be pretty daunting, especially for a beginner, so I thought it might be a good idea to write an article to show how easy it is to get started in WordPress and to design a custom theme.  The first step is to check with your hosting comp…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now