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″]
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
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.
bcworkz
(@bcworkz)
Thank you so much. That worked beautifully. Much appreciate your help.
God bless.
Lode
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