| | 8473 | |
| | 8474 | /** |
| | 8475 | * Retrieves page IDs, permalinks, or titles based on a template file name. |
| | 8476 | * |
| | 8477 | * Queries pages using a specified template file and returns an array of |
| | 8478 | * IDs, permalinks, or titles based on the provided field parameter. |
| | 8479 | * |
| | 8480 | * @param string $template The template file name to search for. |
| | 8481 | * @param string $field The field to return: 'ID' for page IDs, 'permalink' for page permalinks, or 'title' for page titles. |
| | 8482 | * @return array|null An array of IDs, permalinks, or titles of matching pages, or null if no pages are found. |
| | 8483 | */ |
| | 8484 | |
| | 8485 | function get_page_by_template( $template, $field = 'ID' ) { |
| | 8486 | $query = new WP_Query( |
| | 8487 | array( |
| | 8488 | 'post_type' => 'page', |
| | 8489 | 'meta_query' => array( |
| | 8490 | array( |
| | 8491 | 'key' => '_wp_page_template', |
| | 8492 | 'value' => $template, |
| | 8493 | 'compare' => '==', |
| | 8494 | ), |
| | 8495 | ), |
| | 8496 | 'fields' => 'ids', |
| | 8497 | ) |
| | 8498 | ); |
| | 8499 | |
| | 8500 | if ( $query->have_posts() ) { |
| | 8501 | $template_page_ids = $query->posts; |
| | 8502 | |
| | 8503 | if ( 'ID' === $field ) { |
| | 8504 | wp_reset_postdata(); |
| | 8505 | return $template_page_ids; |
| | 8506 | } elseif ( 'title' === $field ) { |
| | 8507 | wp_reset_postdata(); |
| | 8508 | return array_combine( $template_page_ids, array_map( 'get_the_title', $template_page_ids ) ); |
| | 8509 | } elseif ( 'permalink' === $field ) { |
| | 8510 | wp_reset_postdata(); |
| | 8511 | return array_combine( $template_page_ids, array_map( 'get_permalink', $template_page_ids ) ); |
| | 8512 | } |
| | 8513 | } |
| | 8514 | |
| | 8515 | wp_reset_postdata(); |
| | 8516 | return null; |
| | 8517 | } |