Exclude most recent posts from specific category in wp_query()

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' );

Leave a Comment