From 708516c1b7399bc451313e310eda21881bb83a9e Mon Sep 17 00:00:00 2001 From: Gustavo Coimbra Date: Sun, 8 Dec 2024 23:09:23 -0300 Subject: [PATCH] Add get_page_by_template function to retrieve page by template --- src/wp-includes/post.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index eb90e762f5663..a814a4fb96e59 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8467,3 +8467,34 @@ function wp_create_initial_post_meta() { ) ); } + +/** + * Retrieves the ID or permalink of a page based on its template. + * + * This function performs a query to find pages using a specific template + * and returns the ID or permalink of the first matching page. + * + * @param string $template The name of the template to search for. + * @param string $field The field to return: 'ID' for the page ID, 'permalink' for the page permalink. + * @return string|int The ID or permalink of the page, or null if no page is found. + */ +function get_page_by_template( $template, $field = 'permalink' ){ + $query = new WP_Query([ + 'post_type' => 'page', + 'meta_query' => [ + [ + 'key' => '_wp_page_template', + 'value' => $template, + 'compare' => '==' + ] + ] + ]); + while($query->have_posts()){ + $query->the_post(); + if($field == 'ID') { + return get_the_ID(); + } elseif($field == 'permalink') { + return get_permalink(); + } + } +} \ No newline at end of file