Make WordPress Core


Ignore:
Timestamp:
12/14/2021 02:44:46 AM (3 years ago)
Author:
noisysocks
Message:

Filter custom block templates with PHP

This method calls get_block_templates once and uses block template properties
directly for filtering. This way, we can avoid hitting the database for each
public post type.

The previous method is useful when we already know the current post type we
request templates for, like when using REST API.

Follows [52334].
See #54335.
Props mamaduka, youknowriad.

File:
1 edited

Legend:

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

    r52334 r52365  
    12511251
    12521252            if ( current_theme_supports( 'block-templates' ) ) {
     1253                $block_templates = get_block_templates( array(), 'wp_template' );
    12531254                foreach ( get_post_types( array( 'public' => true ) ) as $type ) {
    1254                     $block_templates = get_block_templates( array( 'post_type' => $type ), 'wp_template' );
    12551255                    foreach ( $block_templates as $block_template ) {
     1256                        if ( ! $block_template->is_custom ) {
     1257                            continue;
     1258                        }
     1259
     1260                        if ( isset( $block_template->post_types ) && ! in_array( $type, $block_template->post_types, true ) ) {
     1261                            continue;
     1262                        }
     1263
    12561264                        $post_templates[ $type ][ $block_template->slug ] = $block_template->title;
    12571265                    }
Note: See TracChangeset for help on using the changeset viewer.