Support » Plugins » Hacks » Query posts skip most recent in each category

  • Hi everyone.

    I’m looking for a bit of help using a custom query. I already know the basics and have done a bit of homework on the codex, but I can’t figure this one out. Basically I want to query all most recent posts, in no particular order, including every category. Your basic blog layout. I’ve already got a sort of “widget” higher up in my page which displays the 1 most recent post from 4 specific categories. There’s another feature up there which uses stickies to display featured posts.

    Now, in order to avoid duplicates, ideally I’d like to exclude all posts which are called earlier up using the stickies function (I can already do this) -AND- exclude all posts which are displayed on the widget, mainly the 1 most recent post from 4 specific categories.

    … Is this even possible? I have my 4 “featured” cats in variables, so I can pass them to my query as required, but I can’t figure out how to “jump” the first post from each category and loop the rest (while keeping the stickies out and “older posts” navigation in… ouch).

    If any one out there can help me, it’d be golden. I’m using a slightly modified theme from You can view my work in progress at

    Thanks in advance. 😉


Viewing 5 replies - 1 through 5 (of 5 total)
  • If you can get the post IDs of the 4 most recent posts from the widget, you can use the ‘post__not_in’ parameter of query_posts to exclude them.

    From the Codex

    ‘post__not_in’ => array(6,2,8) – exclusion, lets you specify the post IDs NOT to retrieve

    Yeah, I figured as much. I already tried to do it that way, however my “exclude stickies” function already dumps an array of stickied posts into ‘post__not_in’ and I couldn’t find a way to merge the two.

    … I’m no php crack, so probably there’s an easy way to do it, I just haven’t found it yet.

    Well, there is an array merge function:

    Thanks ambrosite, will try to play with that for a while, see if it gives me what I need. 🙂

    Whoopee! Finally, thanks to some php fiddling, I’ve managed to output exactly what I want. Here’s a recap for those who’d like to do the same thing.

    I want to avoid any and all duplicate posts on the home page.

    I’m using a theme where I have a featured slider at the top of the page. I’ve modified it because it used a featured category and I preferred to use the sticky post function for ease of use. So, that’s variable one, the ids of all sticky posts. My featured slider outputs an array of all post ids it queries through the $ids variable. One down.

    Next, right under the slider, there’s a “featured post from category” area which displays the very latest post from 4 different categories using 4 custom queries. Easy enough to pull the post ids from those. Right at the end of each loop, before wp_reset_query(); I’ve added global $post; $cat1_ID = $post->ID; for each category ($cat_ID1, $cat_ID2, etc…).

    So. That’s all the posts I don’t want duplicated right underneath in the “latest posts” blog area. Using the array merge function ambrosite suggested gives me the following setup:

    		'a' => $cat1_ID,
    		'b' => $cat2_ID,
    		'c' => $cat3_ID,
    		'd' => $cat4_ID,
    $all_ids = $featured_cat_posts + $ids;

    This merges the two arrays and preserves all the post ids without overwriting anything in the two arrays ($featured_cat_posts uses a,b,c,d and $ids uses 0,1,2,3). Then I query again to list all recent posts, using my merged array to exclude duplicates:

    		'post__not_in' => $all_ids,

    Shazaam. It works. Not sure how “grammatically” correct it is, as I’m no php expert, but I’m not getting any errors ;). No duplicates, all content sorted properly. You can see it in action here. Thanks very much to ambrosite for the suggestion. 😀

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Query posts skip most recent in each category’ is closed to new replies.