Make WordPress Core


Ignore:
Timestamp:
09/22/2025 06:48:39 PM (11 months ago)
Author:
westonruter
Message:

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

Follow-up to [60788].

Props mukesh27, westonruter.
See #61097.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.