1

On a category specific template, I want to list all the posts from the last 7 seven days for that single single category and group them by subcategory.

This is the code I have so far, which lists the posts by category, but doesn't limit it to the past week:

<?php
$categories =  get_categories( 'child_of=83' );  
foreach ( $categories as $category ) {
        //Display the sub category information using $category values like $category->cat_name
        echo '<h3>' . $category -> name . '</h3>';
        echo '<ul>';
        foreach ( get_posts( 'cat=' . $category -> term_id ) as $post ) {
            setup_postdata( $post );
            echo '<li><a href="' . get_permalink( $post -> ID ) . '">' . get_the_title() . '</a></li>';   
        }
        echo '</ul>';
    }
wp_reset_postdata(); 
?>

How can I show only published posts dated within the past 7 days, including today?

I thought I would try to use the WP_Query date_query parameter to restrict the list with something like this, but can't figure out how to make this work:

$args = array(
    'date_query' => array(
        'after' => '1 week ago',
        'before' => 'tomorrow',
    ),
    'nopaging' => true,
);
2
1

I would recommend that you use WP_Query for this one:

$categories =  get_categories( 'child_of=83' );  
foreach ( $categories as $category ) {
    echo '<h3>' . $category -> name . '</h3>';
    echo '<ul>';

    // create a WP_Query that retreives all posts from the specified
    // category which is older then 1 week
    $args = array(
      'cat' => $category->cat_ID,
      'date_query' => array(
         array(
          'column' => 'post_date',
          'after' => '1 week ago'
         )
       ),
      'posts_per_page' => -1,
      'post_type' => 'post',
      'post_status' => 'publish'
    );
    $query = new WP_Query($args);

    // check so there is some posts in the resultset
    if($query->have_posts()) {
      // loop through the result
      while($query->have_posts()) {
        $query->the_post();
        // output data here
      }
    }

    echo '</ul>';
}
4
  • This code generated the list of subcategories, but there were two problems: 1) it returned the same lists of posts under each sub-category heading, and 2) it did not limit the posts to only published posts, allowing scheduled posts to be listed too.
    – draney
    Oct 6 '14 at 19:24
  • That seems very strange, have you tried to reset the query after while() loop using: wp_reset_postdata(); I don't have time right now, but will test this for myself tomorrow and update my answer. Oct 6 '14 at 19:58
  • wp_reset_postdata(); doesn't change the output.
    – draney
    Oct 6 '14 at 20:10
  • @draney - Oh, I found one error, you should of course use $category->cat_ID instead of $category->ID, updated my answer. Oct 6 '14 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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