Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Thanks!

share|improve this question
    
I found a plugin that's similar but not exactly what I want to do: goo.gl/kpUv0 –  noel saw Feb 19 '11 at 17:47
add comment

3 Answers

According to docs you can retrieve such set of posts by using Time Parameters with query_posts(), but note that it will likely horribly break your pagination and links to older entries.

share|improve this answer
    
Break pagination? Only in the sense that the result won't be paged without passing in paging values, but the user could always add the paging value into the query, no? –  t31os Feb 18 '11 at 18:53
    
@t31os I had not tested but I don't see easy logic to this. Display variable amount of posts on first page and no more posts (since we limit query to week)... Need either complex offsets or rebuild pagination to have week per page. Either way this is very messy. Disclaimer - I hadn't worked with pagination much, I just think logic is complex to get right here. –  Rarst Feb 18 '11 at 19:21
add comment

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.

share|improve this answer
add comment

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" );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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