Make WordPress Core


Ignore:
Timestamp:
10/11/2022 05:28:54 PM (20 months ago)
Author:
davidbaumwald
Message:

Editor: Ensure WP_Query and WP_Term_Query results are referenced properly when creating dynamic template names for use in the site editor.

[54445] updated the new dynamic template name functions used by the site editor in 6.1 to use WP_Query and WP_Term_Query instances in place of get_posts and get_terms respectively. However, the latter functions return an array of results whereas WP_Query instances store their found results in a class property. This change updates the code to reference either $posts_query->posts or $terms_query->terms where necessary.

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

Props bernhard-reiter, spacedmonkey, davidbaumwald.
See #56467.

File:
1 edited

Legend:

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

    r54470 r54494  
    561561    );
    562562    $args  = wp_parse_args( $args, $default_args );
    563     $posts = new WP_Query( $args );
    564 
    565     if ( empty( $posts ) ) {
     563    $posts_query = new WP_Query( $args );
     564
     565    if ( empty( $posts_query->posts ) ) {
    566566        $template->title = sprintf(
    567567            /* translators: Custom template title in the Site Editor referencing a post that was not found. 1: Post type singular name, 2: Post type slug. */
     
    574574    }
    575575
    576     $post_title = $posts[0]->post_title;
     576    $post_title = $posts_query->posts[0]->post_title;
    577577
    578578    $template->title = sprintf(
     
    592592        'title' => $post_title,
    593593    );
    594     $args = wp_parse_args( $args, $default_args );
    595 
    596     $posts_with_same_title = new WP_Query( $args );
    597 
    598     if ( count( $posts_with_same_title ) > 1 ) {
     594    $args                        = wp_parse_args( $args, $default_args );
     595    $posts_with_same_title_query = new WP_Query( $args );
     596
     597    if ( count( $posts_with_same_title_query->posts ) > 1 ) {
    599598        $template->title = sprintf(
    600599            /* translators: Custom template title in the Site Editor. 1: Template title, 2: Post type slug. */
     
    636635        'slug'   => $slug,
    637636    );
    638     $args  = wp_parse_args( $args, $default_args );
    639     $terms = $term_query->query( $args );
    640 
    641     if ( empty( $terms ) ) {
     637    $args        = wp_parse_args( $args, $default_args );
     638    $terms_query = $term_query->query( $args );
     639
     640    if ( empty( $terms_query->terms ) ) {
    642641        $template->title = sprintf(
    643642            /* translators: Custom template title in the Site Editor, referencing a taxonomy term that was not found. 1: Taxonomy singular name, 2: Term slug. */
     
    649648    }
    650649
    651     $term_title = $terms[0]->name;
     650    $term_title = $terms_query->terms[0]->name;
    652651
    653652    $template->title = sprintf(
     
    670669        'name'   => $term_title,
    671670    );
    672     $args = wp_parse_args( $args, $default_args );
    673 
    674     $terms_with_same_title = $term_query->query( $args );
    675 
    676     if ( count( $terms_with_same_title ) > 1 ) {
     671    $args                        = wp_parse_args( $args, $default_args );
     672    $terms_with_same_title_query = $term_query->query( $args );
     673
     674    if ( count( $terms_with_same_title_query->terms ) > 1 ) {
    677675        $template->title = sprintf(
    678676            /* translators: Custom template title in the Site Editor. 1: Template title, 2: Term slug. */
Note: See TracChangeset for help on using the changeset viewer.