1

I'm having a hard time figuring out how to accomplish this (really simple) task:

Get 4 random posts, but only select from the 10 most recent posts.

This works great for the random part, but obviously selects from all the posts:

    $query = array(
        'posts_per_page' => 4,
        'orderby' => 'rand',
        'post__not_in' =>array($post->ID)
    );
    $wp_query = new WP_Query($query);

Basically I want to take the results of one query:

    $firstquery = array(
        'posts_per_page' => 10,
        'orderby' => 'date',
        'post__not_in' =>array($post->ID),
        'order' => 'DESC'
    );

And pass the results into that first one.

Any help would be super appreciated. I've looked at merging them but I think that just give me results from both? Little confused.

2 Answers 2

0

Run first query las 10 posts, than use array_rand PHP function on posts array:

$args = array(
    'posts_per_page' => 10,
    'post__not_in' =>array( $post->ID ),
    'orderby' => 'post_date',
    'order' => 'DESC'
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {

  if ( $query->post_count > 4 ) {

    $query->posts = array_rand( $query->posts, 4 ); // get 4 random posts

    $query->post_count = 4; // update post count

    // for pagination, if you need it
    $query->max_num_pages = (int) ceil( $query->found_posts / 4 ); 
  }

  // After that loop as usual
  while ( $query->have_posts() ) : $query->the_post();

    // loop here

  endwhile;
  wp_reset_postdata();

}
1
  • The array_rand function is really neat, thanks! I confess I couldn't get this to work - debug kept complaining about a non-object. Nevertheless I really appreciate the answer! I'm sure I just wasn't implementing it correctly.
    – jfree
    Commented May 20, 2014 at 9:16
0

This answer is incorrect

...And I couldn't withdraw it after it had been accepted, please see comments.


You can use wp_get_recent_posts() to get the newest 10 posts, then grab the ids and feed them to your WP_Query's post__in argument.

global $post;

$recent_posts = wp_get_recent_posts( array(
    'numberposts' => 10,
    'exclude' => $post->id,
) );

$recent_posts_ids = array();
foreach( $recent_posts as $recent_post ) {
    $recent_posts_ids[] = $recent_post["ID"];
}

$query_args = array(
    'posts_per_page' => 4,
    'orderby' => 'rand',
    'post__in' => $recent_posts_ids,
);
$wp_query = new WP_Query( $query_args );
3
  • Running 2 db queries instead of one with the only purpose to extract 4 elements from an array is something I really would avoid. Moreover you should never name a custom query variable $wp_query when in global scope, that will override main query messing up things... cc @jfree
    – gmazzap
    Commented May 20, 2014 at 17:21
  • It is not incorrect indeed, it works, just not the best one ;)
    – gmazzap
    Commented May 20, 2014 at 17:51
  • Thanks for the follow up, I've changed the accepted answer. It does work perfectly, though, (as you said) so I'll probably keep it for the time being since I couldn't get the other to work.
    – jfree
    Commented May 22, 2014 at 14:03

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.