0

My category template shows 5 (full content) posts per page and I want to add a list of ALL the posts during the past week for that category to the top of the page as a quick overview. So the first post in the list will be same as first full-content post below the list, etc. except the overview list can have more than 5 posts.

I found this similar answer but it would only show 5 items at a time too, since that is my default posts per page setting.

I also tried using this code, but it shows posts from all categories. I only want to include the current category.

<?php

$args = array(
    'post_type'      => 'post',
    'post_status'    => 'publish',
    'orderby'        => 'date',
    'order'          => 'DESC',
    'date_query'     => array(
                            array(
                            'after' => '1 week ago'
                            )
                        )
);
$my_query = new WP_Query( $args );

if ( $my_query->have_posts() ) {

    echo '<ul>';

    while ( $my_query->have_posts() ) {
        $my_query->the_post();

        echo '<li><a href="' . get_permalink( $post -> ID ) . '">' . get_the_title() . '</a></li>';

    }

    echo '</ul>';

}
wp_reset_postdata();

?>
0

If I'm understanding you correctly you just need two more arguments in your $args array.

Firstly, category_name - see WP_Query - Category Parameter. We get the value from the $wp_query global via get_query_var(), so like this:

'category_name' => get_query_var( 'category_name' )

Secondly, for the number of posts to show there is posts_per_page as it is an archive one could actually use posts_per_archive_page - see WP_Query - Pagination Parameters.

So do either:

'posts_per_page' => -1

Or do:

'posts_per_archive_page' => -1
  • You are a genius. You were able to read my mind and you nailed the answer. Works perfectly. – draney Oct 7 '14 at 22:21

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.