I have the following situation: on my index.php I am showing the most recent 20 posts using WP_Query(). On the same page in another section I would like to display 3 more random posts from the archive that are not among the 20 most recent.

My code (the second WP_Query):

        $archive_random_args = array(
                                        'post_type' => 'post',
                                        'posts_per_page' => 3,
                                        'offset' => 20,
                                        'orderby' => 'rand'

        $archive_random_query = new WP_Query($archive_random_args);
        if ($archive_random_query->have_posts()) {
            while($archive_random_query->have_posts()) {

The problem is that, despite the offset set to 20, posts from the 20 most recent are showing in this second WP_Query loop.

Can offset and 'orderby' => 'rand' be used together as arguments?

PS: Currently I have 36 posts, so there are more than enough posts for the second WP_Query to pick 3 random ones not in the 20 most recent.


1 Answer 1


You need to get the post ids from the original main query and exclude them. Then you should remove your offset from your custom query. That should do the trick. Random ordering basically ignores the offset parameter, so you need to explicitely remove the posts from the query to exclude them

You can use wp_list_pluck() to get an array of post ids from the main query object (using $wp_query->posts)

$posts_ids = wp_list_pluck( $wp_query->posts, 'ID' );

You then need to pass this to your query arguments as the post__not_in parameter

'post__not_in' => $posts_ids,

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.