Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Fixate the random posts on the main page WordPress

  • SOLVED

A specific function has been created to show random posts on the main page. When the "rondom" function is selected, it should still show random posts. But, when the users is visiting the site and they navigate back to the main page, they should see the same "random" post they saw before. When they come back a next time, they see other "random" post. Keep in mind, random is not the last posts.

I want a new function (next to the existing "Random") the allows still to show random posts but only when a new session is created.

Answers (4)

2013-12-04

Arnav Joy answers:

Can you show your file what function you are talking about?


NordicDesign comments:

Hi Arnav,

I have sent you the file. Let me know if you think you can fix it.

KO


NordicDesign comments:

Thanks Arnav! Great work, pleasure working with you.

KO

2013-12-04

John Cotton answers:

The way to do that is to fix the seeding for the randomiser. MySQL supports a constant param for its RAND function.

So adding this code will do the job based on creating a "fixed" value for any given session id. Same session id = same value.


function seed_rand( $orderby ) {
// Make sure we have a session id
if( !session_id() ) session_start();

// Generate a useful integer from the session id - a session id will
// always produce the same number
$seed = hexdec( substr( session_id(), 0, 5 ) );

// Use that 'constant' number as our seed
return "RAND($seed)";
}
add_filter('posts_orderby', 'seed_rand');


Clearly you don't want that running on each query, so you need to be careful about where you put the add_filter. If you are using a frontpage.php template file for instance, you could put the function in functions.php and then the add_filter in the template file.

2013-12-04

akhilesh singh answers:

you can use cookies to show same post :)

2013-12-04

Sébastien | French WordpressDesigner answers:

Could you send me your file of function you are talking about ?
[email protected]


Tell me if I understand your problem :
the list of posts is generated randomly. This list must be the same all long the session.
That's right ?