Opened 8 weeks ago
Last modified 4 weeks ago
#62666 new feature request
Add function to retrieve page ID or permalink based on template
Reported by: | gustavocoimbra | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | trunk |
Component: | General | Keywords: | Enhancement has-patch |
Focuses: | Cc: |
Description
This contribution implements a new function that retrieves the ID or permalink of a page based on its assigned template. The function performs a query to find pages using a specific template and returns either the ID or permalink of the first matching page.
### Details:
- The function
get_page_by_template()
accepts two parameters:$template
(the template name) and$field
(which can be 'ID' for the page ID or 'permalink' for the page URL). - It uses
WP_Query
to search for pages with the specified template. - The function returns either the page ID or permalink based on the
$field
argument.
### Benefits:
- Provides an easy way to retrieve a page by its template, improving flexibility for theme and plugin developers.
- Reduces the need for custom queries in theme files.
- Helps in dynamically linking or referencing pages based on their template.
Attachments (2)
Change History (11)
This ticket was mentioned in PR #7977 on WordPress/wordpress-develop by gustavocoimbradev.
8 weeks ago
#2
- Keywords has-patch added
This contribution implements a new function that retrieves the ID or permalink of a page based on its assigned template. The function performs a query to find pages using a specific template and returns either the ID or permalink of the first matching page.
Trac ticket: https://core.trac.wordpress.org/ticket/62666
This ticket was mentioned in PR #8073 on WordPress/wordpress-develop by @kadiwala.
5 weeks ago
#4
- Keywords has-patch added
Queries pages using a specified template file and returns an array of IDs, permalinks, or titles based on the provided field parameter.
@kadiwala commented on PR #8073:
5 weeks ago
#5
Hi @mukeshpanchal27
Thank you for the suggestion. I've removed that extra space.
gustavocoimbradev commented on PR #8073:
5 weeks ago
#6
ta me tirano kk
gustavocoimbradev commented on PR #8073:
5 weeks ago
#7
ta me tirano kk
#8
@
4 weeks ago
Hi @gustavocoimbra
I've updated the code with an array because what if one template is assigned to multiple pages
So, we need to get all page IDs as per the template.
@kadiwala commented on PR #8073:
4 weeks ago
#9
Hi @mukeshpanchal27
Please review my updated code.
but am not able to create diff file after commit. Can you please suggest me way how can we create diff file if possible.
Thanks
`
/
- Retrieves page IDs, permalinks, or titles based on a template file name. *
- 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;
wp_reset_postdata();
if ( 'ID' === $field ) {
return $template_page_ids;
} elseif ( 'title' === $field ) {
return array_combine( $template_page_ids, array_map( 'get_the_title', $template_page_ids ) );
} elseif ( 'permalink' === $field ) {
return array_combine( $template_page_ids, array_map( 'get_permalink', $template_page_ids ) );
}
}
wp_reset_postdata();
return null;
}
`