Make WordPress Core


Ignore:
Timestamp:
02/13/2008 09:30:26 AM (18 years ago)
Author:
ryan
Message:

Reduce queries by wp_count_posts(). Props josephscott. fixes #5820

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r6803 r6808  
    783783
    784784/**
    785  * wp_count_posts() - Count number of posts with a given type and status
     785 * wp_count_posts() - Count number of posts with a given type
    786786 *
    787787 * {@internal Missing Long Description}}
     
    792792 *
    793793 * @param string $type Post type
    794  * @param string $status Post status
    795  * @return int Number of posts
    796  */
    797 function wp_count_posts( $type = 'post', $status = 'publish' ) {
     794 * @return array Number of posts for each status
     795 */
     796function wp_count_posts( $type = 'post' ) {
    798797    global $wpdb;
    799798
    800     return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s AND post_status = %s", $type, $status) );
     799    $count = $wpdb->get_results( $wpdb->prepare( "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s GROUP BY post_status", $type ), ARRAY_A );
     800
     801    $stats = array( );
     802    foreach( (array) $count as $row_num => $row ) {
     803        $stats[$row['post_status']] = $row['num_posts'];
     804    }
     805
     806    return (object) $stats;
    801807}
    802808
Note: See TracChangeset for help on using the changeset viewer.