Created
          May 29, 2016 06:04 
        
      - 
      
- 
        Save joshuadavidnelson/2f34e512503bf1c9dd37b38aee6f246f to your computer and use it in GitHub Desktop. 
    Grab the page id for the first page using a specific template. Ideal for situations with a template intended for a only one page.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| /** | |
| * Get the first page id with the matching template slug. | |
| * | |
| * @author Joshua David Nelson, [email protected] | |
| * | |
| * @param string $template This is the template slug, like 'template-about-page.php' | |
| * | |
| * @return mixed $page_id Int or false on failure | |
| */ | |
| if( !function_exists( 'get_page_id_by_template' ) ) { | |
| function get_page_id_by_template( $template ) { | |
| $args = array( | |
| 'post_type' => 'page', | |
| 'fields' => 'ids', | |
| 'post_status' => 'publish', | |
| 'nopaging' => true, | |
| 'posts_per_page' => 1, | |
| 'update_post_term_cache' => false, | |
| 'meta_key' => '_wp_page_template', | |
| 'meta_value' => esc_attr( $template ), | |
| ); | |
| $pages = get_posts( $args ); | |
| $page_id = isset( $pages[0] ) ? $pages[0] : false; | |
| return $page_id; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment