Skip to content
Open
45 changes: 45 additions & 0 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -8470,3 +8470,48 @@ function wp_create_initial_post_meta() {
)
);
}

/**
* Retrieves page IDs, permalinks, or titles based on a template file name.
*
* Queries pages using a specified template file and returns an array of
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Queries pages using a specified template file and returns an array of
* Queries pages using a specified template file and returns an array of

* IDs, permalinks, or titles based on the provided field parameter.
*
* @param string $template The template file name to search for.
* @param string $field The field to return: 'ID' for page IDs, 'permalink' for page permalinks, or 'title' for page titles.
* @return array|null An array of IDs, permalinks, or titles of matching pages, or null if no pages are found.
*/

function get_page_by_template( $template, $field = 'ID' ) {
$query = new WP_Query(
array(
'post_type' => 'page',
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => $template,
'compare' => '==',
),
),
'fields' => 'ids',
)
);

if ( $query->have_posts() ) {
$template_page_ids = $query->posts;

if ( 'ID' === $field ) {
wp_reset_postdata();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why reseting post meta in each conditions?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid confliction I've added reset post data function before return

return $template_page_ids;
} elseif ( 'title' === $field ) {
wp_reset_postdata();
return array_combine( $template_page_ids, array_map( 'get_the_title', $template_page_ids ) );
} elseif ( 'permalink' === $field ) {
wp_reset_postdata();
return array_combine( $template_page_ids, array_map( 'get_permalink', $template_page_ids ) );
}
}

wp_reset_postdata();
return null;
}