Support » Fixing WordPress » Display posts of past 7 days broken down by cateogry

  • Hi,

    I’m looking to create a shortcode function that displays all posts of the past 7 days (not including the day of the post) broken down by category. Basically, it’s a post that would go up every Monday morning that’s a wrap up all off posts from the previous Monday-Sunday. I want to make it a shortcode so that the user can write the intro paragraph and then type the shortcode to generate the post.

    How would I go about doing this? I’m not quite sure how to query the posts so that it only pulls from the past 7 days of the publish date (so that if a reader visits the post a week later, it’s not 7 days from when they visit the post, but 7 days from the publish date). Then I’m not sure the best way to loop through to display the category name in an “h4” tag with an unordered list of all post from that category in the previous week.

    Any help would be GREATLY appreciated!

    Thanks,
    Matt

Viewing 15 replies - 1 through 15 (of 19 total)
  • Thread Starter Matt Banks

    (@mjbanks)

    After a lot of trial and error and more Codex reading than I ever thought I’d do on a Saturday, I figured this out. Who knows if anyone else will ever need this, but I figured I would post the code here for reference:

    function display_week_wrapup() {
    
    	//Create a new filtering function that will add our where clause to the query
    	function filter_where($where = '') {
    		$today_date = get_the_date('Y-m-d');
    		//posts between publish date of wrap-up and 7 days prior
    		$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-7 days')) . "' AND post_date < '$today_date'";
    		return $where;
    	}
    	// Register the filtering function
    	add_filter('posts_where', 'filter_where');
    
    	$wrapupcontent = '';
    
    	$categories = get_categories();
    
    	foreach ($categories as $cat) {
    
    		$myposts = query_posts("orberby=date&nopaging=true&cat=$cat->cat_ID");
    
    		if ( have_posts() ) {
    			$wrapupcontent .= "<h4>$cat->cat_name</h4>";
    			$wrapupcontent .= '<ul>';
    			foreach($myposts as $post) {
    				$temp_title = get_the_title($post->ID);
    				$temp_link = get_permalink($post->ID);
    				$wrapupcontent .= "<li><a href='$temp_link'>$temp_title</a></li>";
    			}
    			$wrapupcontent .= '</ul>';
    		}
    
    	} // end foreach cat
    
    	return $wrapupcontent;
    
    }
    add_shortcode( 'weekwrapup' , 'display_week_wrapup' );

    If anyone has optimizations or a better way to do this, let me know 🙂

    Thanks,
    Matt

    I may be wrong, but I think you need to remove the filter function before you return.

    Thread Starter Matt Banks

    (@mjbanks)

    Would that be to reset the query type for any posts below this?

    Yes. Unless you remove the filter, I believe that it will filter all future queries.

    You can check this out by printing out a later query. Use print_r($wp_query->request); just after the query_posts() to see if the date filter is still in place.

    Hello!
    I can read php but do not quite understand. Therefore I will ask for a help …

    I need to show the post, the last week. But I need to start the week on Thursday.

    That is, show the post between Thursday and Thursday.

    Anyone know how?

    Thanks!

    Please give some more information.

    If today is Monday, February 28, what are the dates that you want to see?

    If today were Thursday, February 24, what are the dates that you want to see?

    Thanks for the reply!

    I need to show the post from the day Thursday of past week and WEDNESDAY of current week (7 days), and so on with each week.

    Currently, I have configured Wordporess, for the start of week is Thursday.

    And the loop I use says the following:

    <? php
    $ week = date (‘W’);
    $ year = date (‘Y’);
    query_posts (‘post_status = publish, future & order = ASC & posts_per_page =- 1 & cat = 10.12’. “year = ‘. $ year.’ & w = ‘. $ week) {while (have_posts ()): the_post ();?>

    It does not work, about the day Monday, stop showing the post.

    Not understand what happens, has an idea? As could show all my posts published in the week but the week beginning the day Thursday?

    Thanks again!

    An example: If today is SATURDAY, THURSDAY Need display past, and also the following days until next WEDNESDAY

    THURSDAY FRIDAY SATURDAY SUNDAY MONDAY TUESDAY WEDNESDAY

    The problem is because the PHP date function and the WordPress query_posts functions both count weeks as beginning on Monday and week 1 as the first week of the year that contains a Monday.

    For example, January 1, 2011, was a Saturday so it is counted as being in the last week of 2010, not the first week of 2011. date(‘W’,’strtotime(‘2011/01/01’)) gives 52!

    I will work on using days instead of weeks to do the query and post the results when I have it working.

    Woou … I’m working on this a few days ago and giving him a thousand rounds … and found no solution …

    I would greatly appreciate if you could build a query for this … would be less headache … until I learn more PHP …

    I await your response …
    Thank you very much!

    Here is a working version. It has a little extra code in it, but I left it in to help with debugging while I was testing.

    You will need to adapt that to your own query. Be sure to include the line to turn off the filter after the query.

    function mam_posts_where ($where) {
       global $mam_global_where;
       if ($mam_global_where) $where .= " $mam_global_where";
       return $where;
    }
    add_filter('posts_where','mam_posts_where');
    
    $date = date('Y-m-d');
    $today = date('w',strtotime($date));
    $days_to_next_thursday = ($today < 4) ? 4 - $today : 11 - $today;
    $days_to_last_thursday = ($today < 4) ? -3 - $today : (($today == 4) ? 0: 11 - $today);
    $next_thursday = date('Y-m-d',strtotime("$testdate +$days_to_next_thursday days"));
    $last_thursday = date('Y-m-d',strtotime("$testdate +$days_to_last_thursday days"));
    $mam_global_where = " AND $wpdb->posts.post_date >= '$last_thursday' AND $wpdb->posts.post_date < '$next_thursday'";
    
    query_posts('posts_per_page=2');
    $mam_global_where = '';  // Turn off filter

    Wou! Thas great!

    Now test this code… and tell you if working…

    Insert this code directly in index.php for example? or in function.php file?

    Thanks!

    If it works! I am very happy …
    This code is dynamic? Each week a group of post show again?

    Thank you again for your dedication

    It is dynamic. On Thursday, it will show today’s posts thru next Wednesday. On Friday – Thursday and Friday thru Wednesday. On Saturday – Thursday, Friday, and Saturday thru Wednesday. And so on.

    I think it needs to go in your function after your add_filter and before $wrapupcontent = '';.

    Use your own query_posts(), but add the code to turn off the filter after it.

    You can also change when week starts in Settings of WP.

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Display posts of past 7 days broken down by cateogry’ is closed to new replies.