diff --git a/lib/full-site-editing/page-templates.php b/lib/full-site-editing/page-templates.php index b2d8e795934820..15e261d8d7fa0e 100644 --- a/lib/full-site-editing/page-templates.php +++ b/lib/full-site-editing/page-templates.php @@ -7,11 +7,13 @@ /** * Load the page templates in Gutenberg. - * - * @param array $templates Theme page templates. - * @return array Modified templates. + * + * @param array $templates Page templates. + * @param WP_Theme $theme WP_Theme instance. + * @param WP_Post $post The post being edited, provided for context, or null. + * @return array (Maybe) modified page templates array. */ -function gutenberg_load_fse_page_templates( $templates ) { +function gutenberg_load_fse_page_templates( $templates, $theme, $post ) { if ( ! gutenberg_is_fse_theme() ) { return $templates; } @@ -25,11 +27,16 @@ function gutenberg_load_fse_page_templates( $templates ) { ); $page_templates = array(); if ( isset( $data['pageTemplates'] ) ) { - foreach ( $data['pageTemplates'] as $key => $page_emplate ) { - $page_templates[ $key ] = $page_emplate['title']; + foreach ( $data['pageTemplates'] as $key => $page_template ) { + if ( ( ! isset( $page_template['postTypes'] ) && 'page' === $post->post_type ) || + ( isset( $page_template['postTypes'] ) && in_array( $post->post_type, $page_template['postTypes'], true ) ) + ) { + $page_templates[ $key ] = $page_template['title']; + } } } return $page_templates; } -add_filter( 'theme_page_templates', 'gutenberg_load_fse_page_templates' ); +add_filter( 'theme_post_templates', 'gutenberg_load_fse_page_templates', 10, 3 ); +add_filter( 'theme_page_templates', 'gutenberg_load_fse_page_templates', 10, 3 );