Make WordPress Core

Changeset 54445


Ignore:
Timestamp:
10/10/2022 03:15:31 PM (4 years ago)
Author:
davidbaumwald
Message:

Editor: Dynamic site editor template names performance improvements.

This change updates get_(posts|terms)() to direct WP_Query calls and adds additional parameters to each query to improve performance.

Follow-up to [54280], [54333], [54370], and [54388].

Props spacedmonkey, peterwilsoncc.
See #56467.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r54380 r54445  
    547547        $post_type_object = get_post_type_object( $post_type );
    548548
    549         $posts = get_posts(
    550                 array(
    551                         'name'      => $slug,
    552                         'post_type' => $post_type,
    553                 )
    554         );
     549        $default_args = array(
     550                'post_type'              => $post_type,
     551                'post_status'            => 'publish',
     552                'posts_per_page'         => 1,
     553                'update_post_meta_cache' => false,
     554                'update_post_term_cache' => false,
     555                'ignore_sticky_posts'    => true,
     556                'no_found_rows'          => true,
     557        );
     558
     559        $args  = array(
     560                'name' => $slug,
     561        );
     562        $args  = wp_parse_args( $args, $default_args );
     563        $posts = new WP_Query( $args );
    555564
    556565        if ( empty( $posts ) ) {
    … …  
    580589        );
    581590
    582         $posts_with_same_title = get_posts(
    583                 array(
    584                         'title'       => $post_title,
    585                         'post_type'   => $post_type,
    586                         'post_status' => 'publish',
    587                 )
    588         );
     591        $args = array(
     592                'title' => $post_title,
     593        );
     594        $args = wp_parse_args( $args, $default_args );
     595
     596        $posts_with_same_title = new WP_Query( $args );
    589597
    590598        if ( count( $posts_with_same_title ) > 1 ) {
    … …  
    616624        $taxonomy_object = get_taxonomy( $taxonomy );
    617625
    618         $terms = get_terms(
    619                 array(
    620                         'taxonomy'   => $taxonomy,
    621                         'hide_empty' => false,
    622                         'slug'       => $slug,
    623                 )
    624         );
     626        $default_args = array(
     627                'taxonomy'               => $taxonomy,
     628                'hide_empty'             => false,
     629                'update_term_meta_cache' => false,
     630        );
     631
     632        $term_query = new WP_Term_Query();
     633
     634        $args  = array(
     635                'number' => 1,
     636                'slug'   => $slug,
     637        );
     638        $args  = wp_parse_args( $args, $default_args );
     639        $terms = $term_query->query( $args );
    625640
    626641        if ( empty( $terms ) ) {
    … …  
    649664        );
    650665
    651         $terms_with_same_title = get_terms(
    652                 array(
    653                         'taxonomy'   => $taxonomy,
    654                         'hide_empty' => false,
    655                         'name'       => $term_title,
    656                 )
    657         );
     666        $term_query = new WP_Term_Query();
     667
     668        $args = array(
     669                'number' => 2,
     670                'name'   => $term_title,
     671        );
     672        $args = wp_parse_args( $args, $default_args );
     673
     674        $terms_with_same_title = $term_query->query( $args );
    658675
    659676        if ( count( $terms_with_same_title ) > 1 ) {
Note: See TracChangeset for help on using the changeset viewer.