Solved

Worpdress WP_Query exclude children

Posted on 2011-02-17
8
1,564 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to Import and export files in WordPress. 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 : Click on Too…

920 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

19 Experts available now in Live!

Get 1:1 Help Now