How-to: Random post order [WP]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madtwin
    Confirmed User
    • Aug 2009
    • 274

    #1

    How-to: Random post order [WP]

    I need to randomize all posts on my WP tube, but like every 1-2 days.

    I'm not talking about adding just '&orderby=rand' parameter to the query, it shuffles posts with every page visit/reload and makes some videos appear on page1 and 2...


    Anyone?

  • harvey
    Confirmed User
    • Jul 2001
    • 9266

    #2
    http://codex.wordpress.org/Transients_API
    This post is endorsed by CIA, KGB, MI6, the Mafia, Illuminati, Kim Jong Il, Worldwide Ninjas Association, Klingon Empire and lolcats. Don't mess around with it, just accept it and embrace the truth

    Comment

    • harvey
      Confirmed User
      • Jul 2001
      • 9266

      #3
      optionally, you can use a seed as an argument and store it as a variable so pagination will get the see and go from there. This is a great way to achieve what you're looking for, but I think it won't work well if you're using cache plugins (well, it will work but the purpose of cache plugins would be defeated)

      edit: sorry, now I see you want this to change every couple days, not per session, so just go with Transients API, it's done for this exact purpose
      Last edited by harvey; 05-17-2013, 01:00 AM.
      This post is endorsed by CIA, KGB, MI6, the Mafia, Illuminati, Kim Jong Il, Worldwide Ninjas Association, Klingon Empire and lolcats. Don't mess around with it, just accept it and embrace the truth

      Comment

      • madtwin
        Confirmed User
        • Aug 2009
        • 274

        #4
        Thanks, that Transients API does exactly what I need


        For those interested in this - How do I create a random post that will last for a day ( not tested )

        Comment

        • madtwin
          Confirmed User
          • Aug 2009
          • 274

          #5
          Shit... I can't get it work

          I have standard query on index.php like this:

          <?php if(have_posts()) { ?>

          <?php while (have_posts()) : the_post(); ?>

          <div class="post" id="post-<?php the_ID(); ?>">
          <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_titile(); ?></a>
          </div>

          <?php endwhile; ?>
          So where goes all the Transients API stuff? Something like this?

          <?php
          // Get any existing copy of our transient data
          if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) {
          // It wasn't there, so regenerate the data and save the transient
          $randargs = array('orderby' => 'rand', 'numberposts' => 10);
          $special_query_results = get_posts($randargs);
          set_transient( 'special_query_results', $special_query_results, 60*60*12 );
          }

          // Use the data like you would have normally...
          $randomposts = get_transient( 'special_query_results' );

          global $post;
          foreach( $randomposts as $post ) : setup_postdata($post); ?>

          <div class="post" id="post-<?php the_ID(); ?>">
          <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_titile(); ?></a>
          </div>

          <?php endforeach; ?>
          When using this code I get blank page...

          Comment

          Working...