5

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.

6

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

1
  • 1
    @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
2

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'
);
wp_dropdown_pages($args);

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.

1
  • When I add the meta_key and meta_value arguments to my wp_dropdown_pages function, no element is outputted. The values are correct, and the template file is specified and exists. I Don't believe these arguments are supported.
    – Lee
    Sep 7 '17 at 10:25
2
$query= new WP_Query(array(
    'post_type'  => 'page',  /* overrides default 'post' */
    'meta_key'   => '_wp_page_template',
    'meta_value' => 'page-templates/{template name}.php'
));

This worked for me because my custom theme used page-templates folder.

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.