How do I set up my blog home page to show only this weeke’s posts automatically?

on my WPWeekly.net website, I am currently having to set the number of posts to show manually in the Setup->Reading.

Is there a snippet that will let me have the home page blog show only the current week’s posts automatically?

Read More

Thanks!

Related posts

Leave a Reply

3 comments

  1. I guess i’m not really following Rarst’s concerns regarding paging and prev/next linking, i think you can safely intercept the home page query and change the query vars to look at posts within a specific time range, i tend to favour the filter/action approach(no need to setup paging values).

    add_action( 'pre_get_posts', 'home_week_query' );
    function home_week_query( $wpq ) {
        if( $wpq->is_admin || !$wpq->is_home )
            return;
        if( isset( $wpq->query_vars['post_type'] ) && ( 'nav_menu_item' == $wpq->query_vars['post_type'] ) )
            return;
    
        if( !did_action('wp') )
            return;
    
        $today = getdate();
        $wpq->set( 'year', $today["year"] );
        $wpq->set( 'monthnum', $today["mon"] );
        $wpq->set( 'day', $today["mday"] );
    }
    

    Paging works and the posts are within the specific range, but let me know if you have any problems with the code.

  2. I hired an offshore developer to apply what I wanted, the previous week’s posts on a home page to a Thematic themed site. In retrospect, I should have asked him to apply two weeks (current week + this week):

    You can download the theme here if you want to check it out…
    http://dl.dropbox.com/u/4016505/thematic-wpweekly.net.zip

    But here’s what he did (in his words) – hopes it makes some sense…

    Copy given code into “function.php” file in current theme folder. This will put
    condition to your original query for weekly post.

    function filter_where( $where = '' ) {
            $end_Date = date('Y-m-d',mktime(0,0,0,date('m'),date('d')-date('w')-1 ,date('Y'))) ;
            $start_Date = date('Y-m-d',mktime(0,0,0,date('m'),date('d')-date('w')-7 ,date('Y'))) ;
    
    $where .= " AND post_date >= '".$start_Date."' AND post_date <= '".$end_Date."' ";
    return $where;
    

    }

    Now it will display last week post post but you can modify $end_Date and $start_Date
    variable and set this to current date or any given range of time period.

    if(isset($_GET) and count($_GET)<=0){
        add_filter( 'posts_where', 'filter_where' );
        }
    

    Both code should be write with the other add_filter function and above the given code:

     $locale = get_locale();
    

    If you want to apply all post on the given time period then user given code just
    before the loop

    “have_posts() ) : the_post(); “ 
    

    in extension file in library folder in the current theme folder.

    global $query_string;
    query_posts( $query_string . "&posts_per_page=-1&order=desc" );