diff --git a/src/wp-includes/class-wp-user-query.php b/src/wp-includes/class-wp-user-query.php index fd35182eab9f5..3d2134452bf85 100644 --- a/src/wp-includes/class-wp-user-query.php +++ b/src/wp-includes/class-wp-user-query.php @@ -482,7 +482,7 @@ public function prepare_query( $query = array() ) { $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(); foreach ( $capabilities as $cap ) { if ( in_array( $cap, $role_caps, true ) ) { diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index a197180d9e172..1b1081e317ac5 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8470,3 +8470,45 @@ 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; + 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; +}