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.

Thanks.

link|improve this question

80% accept rate
feedback

1 Answer

up vote 0 down vote accepted

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.

link|improve this answer
Excellent approach, works perfectly. Thanks a lot! – Marcos Jan 21 at 12:49
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.