Make WordPress Core

Changeset 60792

Timestamp:
09/22/2025 06:48:39 PM (3 weeks ago)
Author:
westonruter
Message:

Posts, Post Types: Refactor preparation of query for wp_count_posts().

Follow-up to [60788].

Props mukesh27, westonruter.
See #61097.

Location:
trunk
Files:
2 edited

Legend:

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

    r60788 r60792  
    34073407    ) {
    34083408        // Optimized query uses subqueries which can leverage DB indexes for better performance. See #61097.
    3409         $query = $wpdb->prepare(
    3410             "
     3409        $query = "
    34113410            SELECT post_status, COUNT(*) AS num_posts
    34123411            FROM (
     
    34193418                WHERE post_type = %s AND post_status = 'private' AND post_author = %d
    34203419            ) AS filtered_posts
    3421             ",
    3422             $type,
    3423             $type,
    3424             get_current_user_id()
    3425         );
     3420        ";
     3421        $args  = array( $type, $type, get_current_user_id() );
    34263422    } else {
    3427         $query = $wpdb->prepare(
    3428             "
     3423        $query = "
    34293424            SELECT post_status, COUNT(*) AS num_posts
    34303425            FROM {$wpdb->posts}
    34313426            WHERE post_type = %s
    3432             ",
    3433             $type
    3434         );
    3435     }
    3436 
    3437     $query  .= ' GROUP BY post_status';
    3438     $results = (array) $wpdb->get_results( $query, ARRAY_A );
     3427        ";
     3428        $args  = array( $type );
     3429    }
     3430
     3431    $query .= ' GROUP BY post_status';
     3432
     3433    $results = (array) $wpdb->get_results(
     3434        $wpdb->prepare( $query, ...$args ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Placeholders are used in the string contained in the variable.
     3435        ARRAY_A
     3436    );
    34393437    $counts  = array_fill_keys( get_post_stati(), 0 );
    34403438
  • trunk/tests/phpunit/tests/post.php

    r60788 r60792  
    197197     *
    198198     * @ticket 61097
     199     *
    199200     * @covers ::wp_count_posts
    200201     */
     
    223224        );
    224225
    225         $current_user_id = self::$user_ids['author'];
    226         wp_set_current_user( $current_user_id );
     226        wp_set_current_user( self::$user_ids['author'] );
    227227
    228228        $count = wp_count_posts( $post_type, 'readable' );
Note: See TracChangeset for help on using the changeset viewer.