WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. maerk
    Member
    Posted 8 years ago #

    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?

  2. eje211
    Member
    Posted 8 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.