Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New function to retrieve page IDs/Permalinks/Titles based on template… #8073

Open
wants to merge 11 commits into
base: trunk
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-user-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@
$caps_with_roles = array();

foreach ( $available_roles as $role => $role_data ) {
$role_caps = array_keys( array_filter( $role_data['capabilities'] ) );
$role_caps = (isset($role_data['capabilities'])) ? array_keys( array_filter( $role_data['capabilities'] ) ) : array();

Check failure on line 485 in src/wp-includes/class-wp-user-query.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Expected 1 space after open parenthesis; 0 found

Check failure on line 485 in src/wp-includes/class-wp-user-query.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 485 in src/wp-includes/class-wp-user-query.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Expected 1 spaces before closing parenthesis; 0 found

Check failure on line 485 in src/wp-includes/class-wp-user-query.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Expected 1 space before close parenthesis; 0 found

foreach ( $capabilities as $cap ) {
if ( in_array( $cap, $role_caps, true ) ) {
Expand Down
44 changes: 44 additions & 0 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -8470,3 +8470,47 @@ 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
* 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
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
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;
}
Loading