WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pulling posts from previous week (6 posts)

  1. kevin_curry
    Member
    Posted 3 years ago #

    Okay so I'm trying to modify the following code which works perfectly for pulling all the posts from the previous month and creating a roundup post.

    <?php
    //monthly roundup
    function monthly_roundup( $atts )
    {
    extract(shortcode_atts(array(
    
                'monthnum' => ''
        ), $atts));
        //The Query
            $current_month = (date('m')-1);
            $current_year = date('Y');
            query_posts("year=$current_year&monthnum=$current_month&order=ASC&posts_per_page=-1");
            echo '<h3>';
            echo 'The Month In Review';
            echo '</h3>';
            echo '<div class="round">';
            echo '<ul class="related-entries">';
        //The Loop
        if ( have_posts() ) : while ( have_posts() ) : the_post();
            echo '<li><div class="hover"><a href="'; echo the_permalink(); echo '">'; echo '<h3>'; echo the_title(); echo '</h3><div><span>'; echo comments_number('No Comments','1 Comment','% Comments'); echo '</div></div>'; echo the_post_thumbnail('related');'</a></li>';
        endwhile; else:
        endif;
            echo '</ul>';
            echo '</div>';
     //Reset Query
    wp_reset_query();
    }
    add_shortcode('rndup', 'monthly_roundup'); ?>

    This is what I'm trying but it just shows all the posts from the last year not from the previous week. Anyone know where I'm going wrong?

    <?php
    //weekly roundup
    function weekly_roundup( $atts )
    {
    extract(shortcode_atts(array(
    
                'weeknum' => ''
        ), $atts));
        //The Query
            $current_week = (date('W')-1);
            $current_year = date('Y');
            query_posts("year=$current_year&weeknum=$current_week&order=ASC&posts_per_page=-1");
            echo '<h3>';
            echo 'The Week In Review';
            echo '</h3>';
            echo '<div class="round">';
            echo '<ul class="related-entries">';
        //The Loop
        if ( have_posts() ) : while ( have_posts() ) : the_post();
            echo '<li><div class="hover"><a href="'; echo the_permalink(); echo '">'; echo '<h3>'; echo the_title(); echo '</h3><div><span>'; echo comments_number('No Comments','1 Comment','% Comments'); echo '</div></div>'; echo the_post_thumbnail('related');'</a></li>';
        endwhile; else:
        endif;
            echo '</ul>';
            echo '</div>';
     //Reset Query
    wp_reset_query();
    }
    add_shortcode('weeklyrndup', 'weekly_roundup'); ?>
  2. MichaelH
    Member
    Posted 3 years ago #

    For posts from last year wouldn't you need to decrement the year?

    // don't need this        $current_week = (date('W')-1);
            $current_year = (date('Y')-1);
            query_posts("year=$current_year&order=ASC&posts_per_page=-1");
  3. kevin_curry
    Member
    Posted 3 years ago #

    Sorry for the confusion. I'm trying to show posts from the previous week and current year. But the script I wrote shows posts from every week from the current year.

  4. MichaelH
    Member
    Posted 3 years ago #

    Try just the letter 'w' instead of 'weeknum' in your query

    http://codex.wordpress.org/Template_Tags/query_posts#Time_Parameters

  5. weeknum is not a supported argument for query_posts.

    Use w instead.

    http://codex.wordpress.org/Function_Reference/query_posts#Time_Parameters

  6. kevin_curry
    Member
    Posted 3 years ago #

    Perfect that was the problem. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic