Getting pages filtered by template assigned

I want to retrieve pages with determinate template assigned. Function get_pages() hasn´t any parameter to select by template. For exmaple if I created three pages: Home, Page 1 and Portfolio. After I asigned default template to Home and Page but asigned “portfolio template” to Portfolio Page. How can I get only the pages that are using “portfolio template” for example?

If you know a function to push my in right direction, please advise me.

Read More

Thanks.

Related posts

Leave a Reply

1 comment

  1. Without knowing exactly what you’re trying to acheive, it’s difficult to know if using page templates for this is the best solution, or if you should consider using a Custom Post Type for portfolio items, and then a custom archive page to display them.
    Anyway, I digress…

    The template a page is set to use, is defined in the post meta using the key _wp_page_template. If you use the query_posts method instead of get_pages you can also use the meta values as part of the query.

    In my example, the filename of the Portfolio page template is page-portfolio.php. You’ll need to change the query below to match yours.

    query_posts(array(
        'post_type' =>'page',
        'meta_key'  =>'_wp_page_template',
        'meta_value'=>'page-portfolio.php',
    ));
    

    If you use this query in your template file, you can then use the normal WordPress loop to iterate over the results. You can also add all the other possible parameters to the query to refine it further if you wish.