0

I have a featured posts slider, which uses the 'featured' tag. I have a query for this which grabs the 3 most recent posts with this tag and shows them in the slider. Below this, I have a list of 10 or so most recent posts across all categories. How would I write a query to exclude the 3 most recent posts with the 'featured' tag, as these are already in the slider (don't want post duplication), but then when they drop off the slider (i.e they're now the 4th most recent 'featured' post etc), they appear in the post list?

EDIT: Here is my code - I've written a shortcode so I can drop it into any page/widget/etc I want but the query for getting the 3 most recent 'featured' posts is here:

posts_per_page=3&post_type=post&tag=featured

and the query which grabs the 10 most recent posts across all categories is:

posts_per_page=10&post_type=post

Hope that's helpful :)

Thanks :)

EDIT #2: Used @Chip Bennet's answer, inside my short code which almost works but it only excludes one of the featured posts (most recent I think), not all 3 that are on the slider. Here is the code:

function loopShortcode($atts) {

    $excludeFeaturedPosts  = array(
        'tag' => 'featured',
        'posts_per_page' => 3
    );

    $featured_posts = new WP_Query($excludeFeaturedPosts);

    $featured_post_ids = array();
    foreach ( $featured_posts as $featured_post ) {
        $featured_post_ids[] = $featured_post->ID;
    }

    wp_reset_query();

    // Defaults
    extract(shortcode_atts(array(
        "posts_per_page" => '', // query parameters are defined by the user when you add the shortcode into the page
        "post_type" => ''
    ), $atts));

    $query_args = array(
        "posts_per_page" => $posts_per_page,
        "post_type" => $post_type,
        "post__not_in" => $featured_post_ids
    );

    $listedPosts = new WP_Query($query_args);

    // rest of short code content, e.g loop, output etc

    return $output;
    wp_reset_query();
} // end shortcode function
3
  • 1
    Please edit your question to include your current relevant code. Commented Nov 6, 2013 at 14:26
  • can you also update the code which grabs the 3 most recent post? Commented Nov 6, 2013 at 14:28
  • Where is/are the shortcode(s) defined? Please include that code as well. Commented Nov 6, 2013 at 15:02

1 Answer 1

2

Assuming that the context is the Site Front Page (e.g. a custom front-page template file or custom page template), and not the Blog Posts Index or other archive index pages, the best approach would be to grab the post IDs when you create your slider query, and then specifically exclude them from your custom latest-posts query:

// Slider args
$slider_posts_args = array(
    'tag' => 'featured',
    'posts_per_page' => 3
);
// Slider query
$slider_posts = new WP_Query( $slider_posts_args );
// Grab post IDs
$slider_post_ids = array();
foreach ( $slider_posts as $slider_post ) {
    $slider_post_ids[] = $slider_post->ID;
}

// Recent posts args
$recent_posts_args = array(
    'posts_per_page' => 10,
    'post__not_in' => $slider_post_ids
);
// Recent posts query
$recent_posts = new WP_Query( $recent_posts_args );

If this were the Blog Posts Index, you would need to filter the main query at pre_get_posts, and exclude the posts added to the slider:

function wpse121428_pre_get_posts( $query ) {

    // Blog posts index main query
    if ( is_home() && $query->is_main_query() ) {

        // Slider args
        $slider_posts_args = array(
            'tag' => 'featured',
            'posts_per_page' => 3
        );

        // Slider query
        $slider_posts = new WP_Query( $slider_posts_args );

        // Grab post IDs
        $slider_post_ids = array();
        foreach ( $slider_posts as $slider_post ) {
            $slider_post_ids[] = $slider_post->ID;
        }

        // Remove slider posts from main query
        $query->set( 'post__not_in', $slider_post_ids );
    }
}
add_action( 'pre_get_posts', 'wpse121428_pre_get_posts' );
4
  • Hi Chip. Thanks for the answer, I've edited my post to contain the shortcode stuff and some new code I've written, based on your answer. Unfortunatley, as I've explained above, only one 'featured' post seems to be excluded from the list of 10 posts, not all 3. Thanks a lot if you can help me out again :)
    – Tom Oakley
    Commented Nov 6, 2013 at 17:27
  • What do you get from var_dump( $featured_posts )? (P.S. I doubt it matters here, but your wp_reset_query() calls are unnecessary; they don't actually do anything here.) Commented Nov 6, 2013 at 17:32
  • Hi Chip, var_dump($featured_posts) returns this: cl.ly/SKcY. Hope that's what you were expecting. Yeah I'm never exactly sure when wp_reset_query() is needed so I tend to use it whenever I've used WP_Query() :)
    – Tom Oakley
    Commented Nov 6, 2013 at 17:55
  • That looks like a query object (defining the query parameters), not the return-value queried object (containing the queried posts). Commented Nov 6, 2013 at 18:10

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.