0

To display the most popular posts based on page views I am using this code (source):

<?php
    query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC');
    if (have_posts()) : while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php
    endwhile; endif;
    wp_reset_query();
?>

I would like to restrict the query to return posts from a certin date (i.e. the last seven days) and I am unsure how I would achieve this.

3
  • Please.... don't use bold everywhere... doesn't make your question more important. And please don't use query_posts().
    – kaiser
    Apr 30, 2013 at 14:56
  • I never claimed it made it more important, I was simply highlighting what the actual question was. But I'll change it so it doesn't bother you so much. Also - I thought this site's policy was only constructive answers? Telling me not to use bold and not providing a workable answer isn't exactly constructive. Apr 30, 2013 at 15:05
  • 1
    Have a look at WP_Query() -- there's a snippet of code there labeled Returns posts for just the current week that might be just what you're looking for.
    – Pat J
    Apr 30, 2013 at 15:07

1 Answer 1

1

I've done something in the past using custom fields. Maybe this can get you going in the right direction? This is done directly after the <?php while ( have_posts() ) : the_post(); ?>

<?php
    $currentdate = date("Ymd");
    $expirationdate = genesis_get_custom_field('_racedate');
    $expirestring = str_replace("-","",$expirationdate);
        if (is_null($expirationdate)) {
            $expirestring = '30005050'; //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP;

        } else {

        if (is_array($expirationdate)) {
            $expirestringarray = implode($expirationdate);
        }

        } //else
        if ( $expirestring + 1 > $currentdate ): ?>

This will actually show up everything that has been posted (in my case, future races) and a day after the "racedate", the post would disappear from the loop (it still remains in the dashboard).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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