• Resolved giant slayer

    (@giant-slayer)


    I have used query_posts($query_string . '&orderby=rand'); to randomize the order of the posts within my categories.
    I have my settings to load 9 posts on a page. I click on the “older” navigation link to see more posts in the category, and the second page of navigation is randomized again.

    If I then click on the “newer” nav link, the original randomization is gone.

    Is there a way to randomize once, then have that order remain in tact while navigating through a category using the navigation links?

    Here is a link to my site where you can see what I am talking about when you click any of the menu items.
    https://theviolent.net
    Thanks!!
    –christopher

Viewing 15 replies - 1 through 15 (of 29 total)
  • I have written some sample code that shows how to do this. You can see it here:

    https://wordpress.mcdspot.com/2011/12/24/consistent-random-set-of-posts/

    You will need to modify it to fit your theme.

    Thread Starter giant slayer

    (@giant-slayer)

    Thanks for the response. I’ll look into what you posted.

    Thread Starter giant slayer

    (@giant-slayer)

    Ok. Looking at what you posted, will this work?

    $seed = gregoriantojd(date('h'),date('m'),date('d'),date('Y'));
    $args = array(    'orderby' => rand($seed) );
    query_posts($args);
    Thread Starter giant slayer

    (@giant-slayer)

    hmmm….
    That one did not work. Page one and page two are the same. All the pages are the same.

    Your suggestion will not work. WP ignores the ‘orderby’ unless it exactly matches its known choices, i.e. it will only match ‘rand’, not rand(something).

    You must use the filter to replace the rand(), as I showed in the sample code.

    What benefit/usage is there to randomizing post order?

    Thread Starter giant slayer

    (@giant-slayer)

    I am an artist and want all of my work to have equal exposure and not just the recent works. So when a visitor views any of my category archive pages, it displays a random sampling of images within the gallery.

    You can see it at my site. https://theviolent.net

    vtxyzzy, here is what finally worked

    $seed = gregoriantojd(date('h'),date('m'),date('d'),date('Y'));
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array('orderby' => rand($seed),'paged' => $paged );
    query_posts($query_string .'&'. $args);

    I see…how does this work for SEO?

    @giant slayer: The code you posted gives me a PHP warning:

    Warning: rand() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\test\wp-content\themes\twentyten-child\testphp.php on line 35

    The resulting query does not give a random set for me.

    Thread Starter giant slayer

    (@giant-slayer)

    Yeah, it did not for me either. I thought it did. You are right about the filter. So I will go back to what you posted and try to figure out how to make it work with my stuff.

    Thread Starter giant slayer

    (@giant-slayer)

    One question.
    query_posts($query_string . '&orderby=rand');
    In my original query, I have my $query_string which has values passed to it from WordPress. How do I add the random from your code and still keep the value in the $query_string?

    That query should work just fine as-is.

    Just ignore the $args array in the sample file.

    Thread Starter giant slayer

    (@giant-slayer)

    When I use your query, my category value passed to me from WordPress is lost. It is in the $query_string variable.

    If your query_posts() looks like this:

    query_posts($query_string . '&orderby=rand');

    your original values should not be lost.

    Please put your code into a pastebin and post a link to it here so I can see the entire file.

    Thread Starter giant slayer

    (@giant-slayer)

    Here is the link.
    https://pastebin.com/6AU62BEq

Viewing 15 replies - 1 through 15 (of 29 total)
  • The topic ‘Getting Navigation to work with random post order’ is closed to new replies.