Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a situation where I need to provide a dropdown of pages in a widget, based on whether they are using a specific template. In other words, for all pages using template 'Foo', get the post ID.

I have coded the rest of the widget, but I'm using an input field for entering a page ID (which can get messy with non-technical users, having to figure out the page ID to use) - I would much rather provide a select box with the page title of the pages using the specific template.

I have tried getting the WP_Query object with this:

$the_query = new WP_Query(array(
    'meta_key' => '_wp_page_template',
    'meta_value' => 'templates/_partner.php'

The meta value is corrent (corresponds to 2 entries in the database), but I don't get any results on the widget page. The select box is empty.

Can you not call WP_Query from a widget, or do I need to look elsewhere for this solution?

UPDATE I checked the $the_query->request for the SQL that runs this, and it returns no rows. It turns out that I was missing the 'post_type' => 'page' in the query.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

WP_Query goes only through posts by default.

Try adding page as your post type:

$the_query = new WP_Query(array(
    'post_type'  => 'page',  /* overrides default 'post' */
    'meta_key'   => '_wp_page_template',
    'meta_value' => 'templates/_partner.php'

See: WP_Query - Type Parameters

share|improve this answer
Thanks Maugly, quite embarrassing oversight... :-P –  Calle Oct 30 '12 at 11:06
@Calle no prob - it happens all the time :) I would love to see any as the default value but that would probably make internet explode :P –  Michal Mau Oct 30 '12 at 11:09

You can use wp_dropdown_pages() to directly create a drop-down list of the pages you would like to display:

$args = array(
    'name' => '_partner_page', // "name" attr of the <select> element
    'id' => 'partner-page', // id attr of the <select> element
    'hierarchical' => 1, // set to 0 for flat display
    'echo' => 1, // set to 0 to return
    'selected' => $post->ID, // ID of the option with "selected" attr, set to 0 to disable
    'meta_key' => '_wp_page_template',
    'meta_value' => 'templates/_partner.php'

There are alot of agruments you can pass to wp_dropdown_pages(). Besides the ones on the codex page of this function, you also can pass any argument listed on get_pages() function, as wp_dropdown_pages() uses get_pages() to retrieve the list of pages that will be displayed as dropdown list.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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