1

We have a section on the homepage where we'd like to show a different (random) post every 3 days.

I'm trying to figure out a way to do it. This would give us a weekly post, but I don't know how to get a post every 3 days...:

<?php
            global $post;
            $args = array(
                'post_type' => 'girls_tips',
                'posts_per_page' => 1,
                'no_found_rows' => true, 
                'offset' => (date( 'W' ) - 1)
            );
            $posts = get_posts($args);
            foreach($posts as $post):
            setup_postdata($post);
            ?>

            <h2><?php the_title(); ?></h2>

            <?php endforeach; ?>

Any help would be appreciated! Cheers,

1

Your code example isn't random though, it's a fixed offset, based on a week number.

As stated this isn't something that can be expressed in query API I think.

You would probably have to keep track of state with something like following algorithm:

  1. Query a random post, store its ID
  2. Store a date three days ahead
  3. Check date, if it hadn't passed yet show stored post
  4. If date had passed repeat from the start (and exclude current post from query)
|improve this answer|||||
1

Try this - it should fit your needs. pay attention, I barely tested it... Test yourself before using it on a live live environment.

// calculate times
$second = 1;
$minute = $second * 60;
$hour   = $minute * 60;
$day    = $hour * 24;
$days   = $day * 3;


$time = time(); //get current timestamp
$random_post_timestamp = get_option('my_random_post_timestamp'); //get timestamp of queried post

if( $random_post_timestamp ) {
  $diff = $time - $random_post_timestamp;

  if( $diff >= $days ) { // if timestamp older than 3 days, delete the option
    delete_option('my_random_post');
    delete_option('my_random_post_timestamp');
  }
}

if( get_option('my_random_post') ) { // is there a option with my id?
  //yes, it shoukd be used in my query
  $args = array( 'p' => get_option('my_random_post') ); 
} else {

   //no, query a new one
  $args = array(
    'post_type' => 'post',
    'posts_per_page' => 1,
    'limit' => 1,
    'orderby' => 'rand',
    'meta_key'     => 'already_shown_in_random',
    'meta_compare' => 'NOT EXISTS' //only query posts that don't have a the 'already_shown_in_random' meta
  );
}

// the query
$random_post = new WP_Query($args);
if( $random_post->have_posts() ):
  while( $random_post->have_posts() ): $random_post->the_post();

    if( !get_option('my_random_post') ) { //if not already populated, store my id and the current timestamp in the options
      add_option('my_random_post', get_the_id() );
      add_option('my_random_post_timestamp', time() );

      add_post_meta( get_the_id() , 'already_shown_in_random', 'yes'); //add a post meta so that this post isnt queried a second time
    }

    /* DO YOUR STUFF HERE!!!!! */


  endwhile;
  wp_reset_postdata();
endif;

It does what Rarst postet in his previous answer...

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.