0

Currently I am using a very standard loop, and wondering how can I make it to display something like this "Latest Posts (4 today)" which will update with the amount of posts from that specific category which are published per day.

<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
    <span class="title"><?php the_title(); ?></span>
</a>
</li>
<?php endwhile; else: ?>

    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>
<?php wp_reset_query(); ?>
</ul>

2 Answers 2

0

I think I have figured out the most efficient way to do this. The tricky part is that you need the query for posts within the date range to:

A. Match the current query. If you're looking at a category or tag, you want to get the posts from that day in that category or tag.

B. Not be limited by the current posts-per-page setting. If you post more than one page's worth of posts in a day, the number needs to reflect that.

You also don't need to query all the post data associated with these posts. You're just after the count.

The solution I've come up with is:

  1. Get the current query.
  2. Turn of pagination.
  3. Set it to return just post IDs.
  4. Add a date query to limit it to posts from the current day.

It has been tested and works well.

To do this, we can access global $wp_query then get $wp_query->query_vars for the current query arguments. Then we modify the arguments as I described above and pass the new arguments to get_posts(). Then we just count the results. This is a function that does this:

function wpse_287363_get_posts_today_count() {
    global $wp_query;

    // Get query arguments for current main query.
    $args = $wp_query->query_vars;

    // Set new values for relevant arguments.
    $arg_amendments = array(
        'fields'     => 'ids',
        'nopaging'   => true,
        'date_query' => array(
            array(
                'after'     => 'today',
                'before'    => 'tomorrow - 1 second',
                'inclusive' => true,
            ),
        ),
    );

    // Replace relevant arguments in existing arguments with amended arguments.
    $new_args = array_merge( $args, $arg_amendments );

    // Get posts with new arguments.
    $post_ids = get_posts( $new_args );

    // Count results.
    $count = count( $post_ids );

    // Return count.
    return $count;
}

Now you can use wpse_287363_get_posts_today_count() to get the number of posts made on the current date, relevant to whichever archive you're viewing.

So if you're on a category archive it will return the number of posts made today in that category. On a tag archive it will return the number of posts made today in that tag. It will even work on date archives and show the number of posts made today in a given year/month. If you're just on the blog page it will show the number of posts made today in any category or tag. It should even work with custom post type/taxonomy archives.

0
-1

You can use date_query to get your posts from today,

  1. pre_get_posts

    function function_display_posts_today( $query ) {

    if (!is_admin() && $query->is_main_query()) {
    
        $today = getdate();
    
        $query->set('date_query', array(
                array(
                    'year'  => $today['year'],
                    'month' => $today['mon'],
                    'day'   => $today['mday'],
                ),
            )
        )
    }
    

    } add_action( 'pre_get_posts', 'function_display_posts_today' );

  2. WP_Query

$today = getdate();
$args = array(
  'date_query' => array(
      array(
          'year'  => $today['year'],
          'month' => $today['mon'],
          'day'   => $today['mday'],
      ),
  ),
);
$query = new WP_Query( $args );

See date_query.

UPDATE :

To get number of posts, just write a custom query live above and your count will be,

<?php echo $query->post_count; ?>

Hope this one helps :)

3
  • I am trying to only show the NUMBER of posts per day, not the actual loop for post per day.
    – Herbo
    Nov 30, 2017 at 5:33
  • I have updated my answer Nov 30, 2017 at 5:41
  • This query is going to be limited to whatever the default posts per page setting is, so the answer will be wrong. At the very least use $query->found_posts Nov 30, 2017 at 11:17

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.