WordPress.org

Ready to get started?Download WordPress

Forums

wp query to display one random post from each month in DSC order (1 post)

  1. motocomdigital
    Member
    Posted 2 years ago #

    I'm trying to make this wp query to somehow return one post from each month, and to display all months from the current install. If the post is random that would be awesome. So the posts are different on each page reload.

    See what I've come up with so far...

    <?php $arhiveGrid = new WP_Query(array(
        'post_type'      => 'page'
        'order'          => 'DESC',
        'orderby'        => 'date',
        'posts_per_page' => 99999
    )); ?>
    <?php if ($arhiveGrid->have_posts()) : while ($arhiveGrid->have_posts()) : $arhiveGrid->the_post();
        $retina  = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumb-retina' ); ?>
        <a class="arhive-grid" href="<?php bloginfo('url'); the_time('/Y/m/'); ?>" style="background-image:<?php echo $retina[0]; ?>;">
            <span><?php the_time('F Y') ?></span>
        </a>
    <?php endwhile; unset($arhiveGrid); endif; ?>

    Obviously this query will return every post on my install. The code above is what I'm going to use to build my archive grid for my archives page (not archive.php).

    The grid needs an image from each month as the background, with Month and Year as text overlay, and needs to link to the correct month.

    But code will only work if the query above can be adapted to get a random post from each month.

    Thanks for any help or pointers.

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 2 years ago by motocomdigital
  • This topic is not resolved
  • WordPress version: 3.3.1