Forums

[resolved] Getting Navigation to work with random post order (30 posts)

  1. giant slayer
    Member
    Posted 5 months ago #

    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.
    http://theviolent.net
    Thanks!!
    --christopher

  2. vtxyzzy
    Member
    Posted 5 months ago #

    I have written some sample code that shows how to do this. You can see it here:

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

    You will need to modify it to fit your theme.

  3. giant slayer
    Member
    Posted 5 months ago #

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

  4. giant slayer
    Member
    Posted 5 months ago #

    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);
  5. giant slayer
    Member
    Posted 5 months ago #

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

  6. vtxyzzy
    Member
    Posted 5 months ago #

    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.

  7. SwansonPhotos
    Member
    Posted 5 months ago #

    What benefit/usage is there to randomizing post order?

  8. giant slayer
    Member
    Posted 5 months ago #

    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. http://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);
  9. SwansonPhotos
    Member
    Posted 5 months ago #

    I see...how does this work for SEO?

  10. vtxyzzy
    Member
    Posted 5 months ago #

    @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.

  11. giant slayer
    Member
    Posted 5 months ago #

    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.

  12. giant slayer
    Member
    Posted 5 months ago #

    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?

  13. vtxyzzy
    Member
    Posted 5 months ago #

    That query should work just fine as-is.

    Just ignore the $args array in the sample file.

  14. giant slayer
    Member
    Posted 5 months ago #

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

  15. vtxyzzy
    Member
    Posted 5 months ago #

    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.

  16. giant slayer
    Member
    Posted 5 months ago #

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

  17. giant slayer
    Member
    Posted 5 months ago #

    I commented out my original query_posts and used the code you sent.

  18. vtxyzzy
    Member
    Posted 5 months ago #

    Please change this part of the code:

    $mam_posts_query = " ORDER BY rand($seed) "; // Turn on filter
    $args = array(    'caller_get_posts' => 1,  // Stickies will be repeated if this is not set
        'orderby' => 'rand',      // This MUST be in the query - the filter checks for it
        'paged' => $paged );
    query_posts($args);
    $mam_posts_query = ''; // Turn off filter 
    
    //query_posts($query_string . '&orderby=rand');

    to this:

    $mam_posts_query = " ORDER BY rand($seed) "; // Turn on filter
    query_posts($query_string . '&orderby=rand');
    $mam_posts_query = ''; // Turn off filter
  19. giant slayer
    Member
    Posted 5 months ago #

    hmmmm....
    I tried it and still no success. Here is my code.

    // Retrieve consistent random set of posts with pagination
    function mam_posts_query($query) {
        global $mam_posts_query;
        if ($mam_posts_query && strpos($query, 'ORDER BY RAND()') !== false) {
           $query = str_replace('ORDER BY RAND()',$mam_posts_query,$query);
        }
        return $query;
    }
    add_filter('query','mam_posts_query'); 
    
    $seed = gregoriantojd(date('h'),date('m'),date('d'),date('Y'));
    global $mam_posts_query;
    $mam_posts_query = " ORDER BY rand($seed) "; // Turn on filter
    query_posts($query_string . '&orderby=rand');
    $mam_posts_query = ''; // Turn off filter

    If you visit any of the menu items from my page, you can see that they are still random, but are still re-randomized when visiting 2nd and 3rd pages. Each refresh is also randomizing.

    http://theviolent.net/colorstudy-ricepaper

  20. vtxyzzy
    Member
    Posted 5 months ago #

    Time for a little debugging. Please add these lines just after your query_posts() so the actual query can be seen:

    global $wp_query;
    print_r("<p>QUERY:$wp_query->request</p>");
  21. giant slayer
    Member
    Posted 5 months ago #

    For my inspirational gallery, this is the query which was printed for page 1.

    QUERY:
    SELECT SQL_CALC_FOUND_ROWS wp_posts.*
    FROM wp_posts
    INNER JOIN wp_term_relationships
    ON (wp_posts.ID = wp_term_relationships.object_id)
    WHERE 1=1 AND
    ( wp_term_relationships.term_taxonomy_id IN (76) ) AND
    wp_posts.post_type = 'post' AND
    (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
    GROUP BY wp_posts.ID
    ORDER BY RAND() DESC LIMIT 0, 9

    It does not appear the rand() function is receiving the seed.

  22. vtxyzzy
    Member
    Posted 5 months ago #

    I am really handicapped here because I can't see the entire template file. Please put the entire file in a pastebin and post a link to it here.

  23. giant slayer
    Member
    Posted 5 months ago #

  24. vtxyzzy
    Member
    Posted 5 months ago #

    OK, more debugging to make sure filter is getting called. Please place the following line just before the 'return $query;' line in the filter;

    print_r('<p>IN FILTER: $mam_query_posts<br />QUERY: $query</p>');

  25. giant slayer
    Member
    Posted 5 months ago #

    Ok, here is what it printed.

    IN FILTER: ORDER BY rand()
    QUERY:SELECT wp_term_taxonomy.term_id
    FROM wp_term_taxonomy
    INNER JOIN wp_terms USING (term_id)
    WHERE taxonomy = 'category'
    AND wp_terms.slug IN ('inspirational')
    
    IN FILTER: ORDER BY rand()
    QUERY:SELECT term_taxonomy_id
    FROM wp_term_taxonomy
    WHERE taxonomy = 'category'
    AND term_id IN (69)
    
    IN FILTER: ORDER BY rand()
    QUERY:SELECT t.*, tt.*
    FROM wp_terms AS t
    INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id
    WHERE tt.taxonomy = 'category'
    AND t.slug = 'inspirational' LIMIT 1
    
    IN FILTER: ORDER BY rand()
    QUERY: SELECT SQL_CALC_FOUND_ROWS  wp_posts.*
    FROM wp_posts  INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    WHERE 1=1
    AND ( wp_term_relationships.term_taxonomy_id IN (76) )
    AND wp_posts.post_type = 'post'
    AND (wp_posts.post_status = 'publish'
    OR wp_posts.post_status = 'private')
    GROUP BY wp_posts.ID  ORDER BY rand()
    DESC LIMIT 0, 9
    
    IN FILTER: ORDER BY rand()
    QUERY:SELECT FOUND_ROWS()

    It looks like the function got called 9 times, once for each image(post) being displayed.

  26. vtxyzzy
    Member
    Posted 5 months ago #

    OK - here is the problem. You added another parameter to the call to gregoriantojd:

    $seed = gregoriantojd(date('h'),date('m'),date('d'),date('Y'));

    That function can only take 3 arguments, so it is failing. If you want things to change every hour, replace that line with this:

    $seed = gregoriantojd(date('m'),date('d'),date('Y')) . date('h');

  27. giant slayer
    Member
    Posted 5 months ago #

    That worked perfectly. Thanks so much for your help. If there is anything I can do in return, please let me know, and I will be glad to do what I can.

  28. giant slayer
    Member
    Posted 5 months ago #

    Mac,
    If you would like, for your help, I'd love to send you a 5x7 print of one of my fine art pieces. If you would like to browse through my work and pick a piece you like, I will gladly mail it out to you.
    You can email the title of the one you want along with your address at cmgdesignstudios[at]theviolent.net

  29. vtxyzzy
    Member
    Posted 5 months ago #

    Thanks for the kind offer. I will look into it tomorrow.

  30. vtxyzzy
    Member
    Posted 5 months ago #

    One final point. It will be slightly more efficient if you replace this:

    $seed = gregoriantojd(date('m'),date('d'),date('Y')) . date('h');

    with this:

    $seed = date('Ymdh');

Reply

You must log in to post.

About this Topic