WordPress.org

Ready to get started?Download WordPress

Forums

Query posts skip most recent in each category (6 posts)

  1. roelani
    Member
    Posted 3 years ago #

    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 elegantthemes.com. You can view my work in progress at http://wordinprogress.com/dev.

    Thanks in advance. ;)

    -roelani

  2. ambrosite
    Member
    Posted 3 years ago #

    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
    http://codex.wordpress.org/Function_Reference/query_posts

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

  3. roelani
    Member
    Posted 3 years ago #

    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.

  4. ambrosite
    Member
    Posted 3 years ago #

    Well, there is an array merge function:

    http://php.net/manual/en/function.array-merge.php

  5. roelani
    Member
    Posted 3 years ago #

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

  6. roelani
    Member
    Posted 3 years ago #

    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:

    $featured_cat_posts=array(
    		'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:

    $args=array(
    		'post__not_in' => $all_ids,
    		'paged'=>$paged,);
    query_posts($args);

    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. :D

Topic Closed

This topic has been closed to new replies.

About this Topic