Skip to content

Commit

Permalink
Add get_page_by_template function to retrieve page by template
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavocoimbradev committed Dec 9, 2024
1 parent faff3a2 commit 708516c
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ){

Check failure on line 8481 in src/wp-includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Expected 1 space before opening brace; found 0
$query = new WP_Query([

Check failure on line 8482 in src/wp-includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 8482 in src/wp-includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Opening parenthesis of a multi-line function call must be the last content on the line

Check failure on line 8482 in src/wp-includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Short array syntax is not allowed
'post_type' => 'page',

Check failure on line 8483 in src/wp-includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Tabs must be used to indent lines; spaces are not allowed
'meta_query' => [

Check failure on line 8484 in src/wp-includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 8484 in src/wp-includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Short array syntax is not allowed
[

Check failure on line 8485 in src/wp-includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 8485 in src/wp-includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Short array syntax is not allowed
'key' => '_wp_page_template',

Check failure on line 8486 in src/wp-includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Tabs must be used to indent lines; spaces are not allowed
'value' => $template,
'compare' => '=='
]
]
]);
while($query->have_posts()){
$query->the_post();
if($field == 'ID') {
return get_the_ID();
} elseif($field == 'permalink') {
return get_permalink();
}
}
}

0 comments on commit 708516c

Please sign in to comment.