WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display only 1 random post from the most recent 20 (10 posts)

  1. f1ss1on
    Member
    Posted 11 months ago #

    Hello, I am trying to display only 1 random post from the most recent 20 posts. Currently, this is the code I have. The issue is, only the latest post is displaying since showposts =>1.

    $post_id = get_the_ID();
        $args = array(
          'orderby' => 'post_date',
          'orderby' => 'rand',
          'showposts'=>1,
          'post__not_in' => array($post_id),
          'meta_query' => array(array('key' => '_thumbnail_id'))
        );
    
        $rand_query = new WP_Query($args);
    
        while ($rand_query->have_posts()) : $rand_query->the_post();
  2. esmi
    Forum Moderator
    Posted 11 months ago #

    I'm not sure what the problem is. You said that you only want 1 random post, yes. Isn't that what you are getting?

  3. f1ss1on
    Member
    Posted 11 months ago #

    Yes it is, but it is only getting the most recent post. I need it to randomly display from the most recent 20 posts

  4. esmi
    Forum Moderator
    Posted 11 months ago #

    You cannot have two orderby parameters like that. Try:

    $post_id = get_the_ID();
        $args = array(
          'orderby' => 'post_date rand',
          'showposts'=>1,
          'post__not_in' => array($post_id),
          'meta_query' => array(array('key' => '_thumbnail_id'))
        );

    http://codex.wordpress.org/Function_Reference/WP_Query#Order_.26_Orderby_Parameters

  5. f1ss1on
    Member
    Posted 11 months ago #

    It is functioning the same as I had it before now by displaying random posts from all posts, not the most recent 20. So now we just need it to grab posts from only the most recent 20. :D

  6. esmi
    Forum Moderator
    Posted 11 months ago #

    So you want only 1 of the recent 20 posts displayed, yes? If so, you'll need to amend the query to pull all 20 recent posts.

    $post_id = get_the_ID();
        $args = array(
          'orderby' => 'post_date rand',
          'posts_per_page'=> 20,
          'post__not_in' => array($post_id),
          'meta_query' => array(array('key' => '_thumbnail_id'))
        );

    Then grab a random post from the returned query object.

  7. f1ss1on
    Member
    Posted 11 months ago #

    No, thats not working either. Now what it is doing is displaying all 20 posts. I only need 1 post to randomly display out of the 20 most recent posts.

  8. esmi
    Forum Moderator
    Posted 11 months ago #

    Then grab a random post from the returned query object.

  9. f1ss1on
    Member
    Posted 11 months ago #

    I figured it out. I wrote a separate function calling the most recent posts using wp_get_recent_posts then just queried it out into randomly displaying 1. Thanks for your help.

  10. lukemv
    Member
    Posted 11 months ago #

    Care to share the code?

Reply

You must log in to post.

About this Topic