WordPress.org

Support

Support » Plugins and Hacks » How do I get the number of posts in a month, day or year, or for a search query?

How do I get the number of posts in a month, day or year, or for a search query?

  • Is there a quick way to get the number of posts in a particular month, day or year?

    And is there a way to get the number of posts returned in search results?

    You can do this with categories:

    $this_cat = &get_category($wp_query->get_queried_object_id());
    $categoryPostCount = $this_cat->category_count;

    I was wondering whether there was something similar for getting the number of posts in date-based archives and searches?

Viewing 1 replies (of 1 total)
  • You could do that via mySQL. Something like:

    global $wpdb;

    $last_day_of_this_month =
        date('d',
            mktime(0, 0, 0,
            the_time('m') + 1,
            0, the_time('Y'))
        );

    $this_month_begin =
        the_time('Y') . '-' .
        the_time('m') . '-01';
    $this_month_end =
        the_time('Y') . '-' . the_time('m') . '-' .
        $last_day_of_this_month;

    $this_month_post_count =
        $wpdb->get_var(
            "SELECT COUNT(*)
            FROM $wpdb->posts
            WHERE post_date BETWEEN this_month_begin AND $this_month_end
            AND post_status = 'publish'"
        );

    Though I haven’t tested this.

Viewing 1 replies (of 1 total)
  • The topic ‘How do I get the number of posts in a month, day or year, or for a search query?’ is closed to new replies.
Skip to toolbar