Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Worpdress WP_Query exclude children

Posted on 2011-02-17
8
Medium Priority
?
1,660 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 1200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

610 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