• Hi;

    How can I display four specific posts in random order every time the website has been refreshed?

    I know the orderby=rand option but that doesn’t accomplish what I wish to do. I want to display only the four latest posts in random order, the rest of the posts I perform another query on.

    I used ‘posts_per_page=4&orderby=rand but that gives me four random posts. Again not what I wish to accomplish.

    Any suggestions are much appreciated.

    Thank you

    Lode

    • This topic was modified 4 years, 2 months ago by Jan Dembowski.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • How can I display four specific posts in random order every time the website has been refreshed?

    Did you try this plugin?

    https://wordpress.org/plugins/random-post-on-refresh/

    It lets you do what you are asking:

    ids – Provide a comma-separated list of post IDs to pull random posts from. Example: [random_post_on_refresh ids=”19, 87, 113, 997″]

    Thread Starter ndjworldnews

    (@ndjworldnews)

    Hello a2hostingrj

    (@a2hostingrj)

    Thank you for the quick response. Unfortunately this plugin does not do what I want to accomplish. My goal is to be able to display the last four posts I published with post one being displayed with a big picture, post 2 a medium picture and post 3 and 4 with a small picture (I got that done). When you refresh the page those exact same posts should then be shuffled around randomly and for argument’s sake post #3 takes the place of post #1 and post #2 replaces post #4 and post #1 becomes #3 and #1 becomes #3. The page gets refreshed again and those same posts shuffle around yet again, displaying in different order yet again. That’s what I am looking to do.

    Thanks for you answer. I do appreciate it.

    Lode

    Moderator bcworkz

    (@bcworkz)

    In your query arguments, include the “post__in” argument, setting its value as an array of the 4 post IDs you want to be randomly ordered.

    You can use “post__not_in” with the same array to exclude the posts from your other query if you wish.

    Thread Starter ndjworldnews

    (@ndjworldnews)

    bcworkz

    (@bcworkz)

    Thank you so much. That worked beautifully. Much appreciate your help.

    God bless.

    Lode

    Thread Starter ndjworldnews

    (@ndjworldnews)

    UPDATE:

    Using BCWORKS suggesting of using the ‘post__in’ argument in my array I got the function to work perfectly. Thanks again BCWORKS!

    But … it means that I have to manually update the post__in array every time I publish a new post. Not very convenient plus I might forget.

    After tweaking around I bit I came up with an automated solution that does not require manually updating the array. Here’s the code for anyone who has a need for it.

    $post_list = get_posts( array(
    ‘numberposts’ => ‘4’
    ) );
    $posts = array();
    foreach ( $post_list as $post ) {
    $posts[] += $post->ID;
    }

    $arr=array($posts[0],$posts[1],$posts[2],$posts[3]);
    $args = array(
    ‘post_type’ => ‘post’,
    ‘category_name’ => $featured_cat_name,
    ‘post__in’ => $arr,
    ‘orderby’ => ‘rand’
    );
    query_posts($args);

    Thanks again for the support.

    God bless;

    Lode

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Random display of specific posts’ is closed to new replies.