4

Say I have 3 categories: A, B, C, and another category D that posts in A,B,C can also be assigned to. D is to denote the featured post for the category, and theoretically only one post for each (A, B, C) will also be assigned D.

I can find the featured post for each category fine using category__and .

I have a need to make a list of the rest of the category minus the featured post to the right of the featured post on the home page. So for example I want to get a list of category A but I want to exclude the featured post that would be marked category A and category D.

I tried using "category_in" followed by "category_not_in" thinking it would accomplish this but the "category__not_in" seems to override and just find everything in every category except for the excluded category.

If I have things structured wrong please let me know. I am trying to make things simple for the site owner by not having to remember to enter text for a tag (featured post). I'd rather them just check the check boxes in the category list.

Summarizing, my code needs to do two things. For example:

Find the "featured post": FIND A and D . This is done. Find everything in the category EXCEPT featured post: Find A, exclude (A and D).

Any help would be greatly appreciated. I'm new to Wordpress but not coding. Just trying to get acquainted with the functionality.

2
  • 1
    As G.M. mentioned below, is there a reason you're not using sticky posts for your 'featured' items (instead of a separate category)? codex.wordpress.org/Sticky_Posts
    – Andy Adams
    Aug 30, 2013 at 16:53
  • Don't use query_posts, there is no valid reason to use it when WP_Query and the pre_get_posts filter are available, or even get_posts
    – Tom J Nowell
    Apr 19, 2014 at 18:56

1 Answer 1

1

This is probably not a "pure" way to do it, but this code make an array of featured post ids and then excludes them from future get_posts.

$featuredPosts = get_posts('category'=>'D','numberposts'=>'-1');
$fPId = array();
foreach ($featuredPost as $currentPost)
{
  array_push($fPId,$currentPost->ID);
}
$aPosts = get_posts('category' => 'a', 'exclude'=>$fpId);
$bPosts = get_posts('category' => 'b', 'exclude'=>$fpId);
$cPosts = get_posts('category' => 'c', 'exclude'=>$fpId);
3
  • Ah! I didn't even think of taking that angle. I'll give it a try. Thanks! Jul 19, 2012 at 6:30
  • @CraigCoston Fixed a syntax error
    – fdsa
    Jul 19, 2012 at 22:10
  • 2
    Wordpress as a core function sticky posts that is intended for that purpose. Get only sticky posts: 'post__in' => get_option( 'sticky_posts' ), exclude stycky posts: 'post__not_in' => get_option( 'sticky_posts' ) faster, simpler and most reliable on theme changing.
    – gmazzap
    Jul 26, 2013 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.