I would like to know how I can loop through pages with a specific template name. I have several pages where the admin can select a template name of SALE. I want to display all content with this template name via the loop.

Any ideas are much appreciated!

  • Kindly refer : wordpress.stackexchange.com/questions/46582/… – Helping Hands Dec 29 '14 at 11:29
  • @HelpingHands thanks, but as far I can see its an example with page IDs. I want to display the pages via template! Could you still help me with this? – user49869 Dec 29 '14 at 12:03
  • Does that link not helping you? shared by @Bainternet?? – Helping Hands Dec 29 '14 at 12:05
  • 1
    @HelpingHands sorry but what link to you mean? Cannot see a user named Bainternet. – user49869 Dec 29 '14 at 14:53

Wordpress saves page templates that is assigned to a specific page in the db in the wp_postmeta table.

These are saved as follows:

'meta_key' => '_wp_page_template',
'meta_value' => 'NAME_ OF_TEMPLATE'

With this in mind, you can loop through pages which shares a specific page template, using get_pages (You can also make use of WP_Query)


$pages = get_pages(array(
    'meta_key' => '_wp_page_template',
    'meta_value' => 'page-special.php'
foreach($pages as $page){
    echo $page->ID.'<br />';
    echo apply_filters( 'the_content', $page->post_content );
| improve this answer | |
  • thanks, this works well. Only one thing: if I would like to have every content of $page would I do echo $page->the_content? Because this gives me an error. – user49869 Jan 1 '15 at 17:28
  • Check my update. You have to make use of the WP_Post objects. You cannot use the normal template tags :-) – Pieter Goosen Jan 1 '15 at 17:31
  • My pleasure, glad it worked. Enjoy :-) – Pieter Goosen Jan 1 '15 at 17:35

Use get_template_part, for example if your template is named sale.php you can use:

get_template_part( 'sale' );  


| improve this answer | |
  • thanks but this does not really solve what I intend, or I dont understand it as I should.. basically what I want to do is do a new query and instead of selecting a specific post, I select a specific template: new WP_Query("template_name=sale"); something like this? – user49869 Dec 31 '14 at 12:54
  • That doesn't really make any sense, WordPress info is stored in a database and not a template. – Wyck Dec 31 '14 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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