Support » Plugins » Hacks » Random post query – include all posts of a random year

  • Im trying to create a query that shows all the posts from a random year. So when a visitor comes to the page they get a random year with all the posts from that year.

    Im creating an historic site with a post for each month of the year so each random year will always have 12 posts. Eg each year 1986, 1987 etc. has 12 posts.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You can do something like this:
    query_posts('year='.rand(1986, 2011));

    Thanks for the reply

    I tried a basic loop just to see if it works but I get an error

    <?php query_posts ('year='.rand(1986, 2011)); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php the_title(); ?>
    <?php endwhile; ?>
    <?php endif; ?>

    Parse error: syntax error, unexpected ‘;’, expecting T_STRING or T_VARIABLE or ‘$’ in /home/content/30/8250330/html/ on line 58

    Line 58 being the first line, query.

    Ok I think I got it now, was some charater changes when I copy pasted it.

    Thanks for your help seems like an easy solution.

    // The Query
    query_posts ('year=' . rand(1986, 2011));
    // The Loop
    while ( have_posts() ) : the_post();
    	echo '<li>';
    	echo '</li>';
    // Reset Query


    Forum Moderator


    query_posts ('year=' . rand(1986, date('Y')-1));

    should automatically expand this to the last full year, excluding the current year (untested).

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Random post query – include all posts of a random year’ is closed to new replies.