Make WordPress Core

Ticket #16264: get_pages_by_template.diff

File get_pages_by_template.diff, 1.3 KB (added by mikeschinkel, 14 years ago)

Adds get_pages_by_template() and fixes a phpdoc comment typo

  • wp-includes/post.php

     
    50095009 * @uses wp_find_hierarchy_loop()
    50105010 *
    50115011 * @param int $post_parent ID of the parent for the post we're checking.
    5012  * @parem int $post_ID ID of the post we're checking.
     5012 * @param int $post_ID ID of the post we're checking.
    50135013 *
    50145014 * @return int The new post_parent for the post.
    50155015 */
     
    52355235}
    52365236add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
    52375237
    5238 ?>
    5239  No newline at end of file
     5238/**
     5239 * Return array of posts for post_type='page' for those pages with the given page template.
     5240 *
     5241 * @param string $template Filename of the assigned template for a page.
     5242 *
     5243 * @return array The posts that have a template matching the one passed.
     5244 */
     5245function get_pages_by_template( $template ) {
     5246        global $wpdb;
     5247        $sql = "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='_wp_page_template' AND meta_value LIKE '%s'";
     5248        $sql = $wpdb->prepare( $sql, $template . '%' );  // '%' to allow partial matches
     5249        $pages = $wpdb->get_col($sql);
     5250        foreach( $pages as $index => $page_id )
     5251                $pages[$page_id] = get_post( $page_id );
     5252        return apply_filters( 'get_pages_by_template', $pages, $template );
     5253}
     5254
     5255?>