diff --git a/lib/template-parts.php b/lib/template-parts.php index 8ab2d745b193e..7edf730bcaacd 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -138,3 +138,42 @@ function gutenberg_render_template_part_list_table_column( $column_name, $post_i echo esc_html( $post->post_name ); } add_action( 'manage_wp_template_part_posts_custom_column', 'gutenberg_render_template_part_list_table_column', 10, 2 ); + + +/** + * Filter for adding a `theme` parameter to `wp_template_part` queries. + * + * @param array $query_params The query parameters. + * @return array Filtered $query_params. + */ +function filter_rest_wp_template_part_collection_params( $query_params ) { + $query_params += array( + 'theme' => array( + 'description' => __( 'The theme slug for the theme that created the template part.', 'gutenberg' ), + 'type' => 'string', + ), + ); + return $query_params; +} +apply_filters( 'rest_wp_template_part_collection_params', 'filter_rest_wp_template_part_collection_params', 99, 1 ); + +/** + * Filter for supporting the `theme` parameter in `wp_template_part` queries. + * + * @param array $args The query arguments. + * @param WP_REST_Request $request The request object. + * @return array Filtered $args. + */ +function filter_rest_wp_template_part_query( $args, $request ) { + if ( $request['theme'] ) { + $meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array(); + $meta_query[] = array( + 'key' => 'theme', + 'value' => $request['theme'], + ); + + $args['meta_query'] = $meta_query; + } + return $args; +} +add_filter( 'rest_wp_template_part_query', 'filter_rest_wp_template_part_query', 99, 2 ); diff --git a/packages/block-library/src/template-part/edit/use-template-part-post.js b/packages/block-library/src/template-part/edit/use-template-part-post.js index 23b034b722462..7ef0f705da2dc 100644 --- a/packages/block-library/src/template-part/edit/use-template-part-post.js +++ b/packages/block-library/src/template-part/edit/use-template-part-post.js @@ -28,7 +28,7 @@ export default function useTemplatePartPost( postId, slug, theme ) { { status: [ 'publish', 'auto-draft' ], slug, - meta: { theme }, + theme, } ); const foundPosts = posts?.filter(