Make WordPress Core

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's profile 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)

62666.diff (1.6 KB) - added by kadiwala 5 weeks ago.
Created pull request in trunk
62666.2.diff (1.6 KB) - added by kadiwala 5 weeks ago.
removed extra space as suggested.

Download all attachments as: .zip

Change History (11)

#1 @gustavocoimbra
8 weeks ago

<?php
/**
 * 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();
        }
    }
}

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
5 weeks ago

Created pull request in trunk

@kadiwala
5 weeks ago

removed extra space as suggested.

@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 @kadiwala
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;

}
`

Note: See TracTickets for help on using tickets.