0

I've seen this solution for querying pages based off of template type: Page template query with WP_Query

I'm having trouble getting it to work with posts, that are using a "Single Post Template:" rather than a "Template Name:" definition.

Here is my code:

 // query
$the_query = new WP_Query( array(
  'post_type' => 'post',
  'posts_per_page'  => -1,
  'meta_query' => array(
     array(
     'key' => '_wp_page_template', 
     'value' => 'single-current.php'
     )
   )
));

Is there a different 'key' I should be using?

Something like '_wp_post_template'

Thanks.

  • I don't think _wp_page_template gets stored unless you're explicitly changing it. Can you try a query where the template is NOT definition? – WebElaine Feb 1 at 21:33
0

If you are still stuck, one possible alternative is to get all Post ID's by the custom post type. You could try something like this:

$the_query = new WP_Query( array(
  'post_type' => 'my-custom-post',
  'posts_per_page'  => -1,
   )
));
while ( $the_query->have_posts() ) : $the_query->the_post();
    the_ID();
endwhile;

It would require you creating a custom post type, and associating posts to it.

There are ways you can associate page templates to posts, but as far as how WP_Query would work from that point with that process, I still have to learn :)

  • 1
    Well technically, yes, you can associate a custom template to a post if the template has Template Post Type: post in the header, but only these custom values get stored in _wp_page_template. – Milo Feb 1 at 22:09
  • ahh you are right! My bad! I updated my answer. – klewis Feb 1 at 22:17
0

I recently ran into a situation where I needed to query posts in this same way. Post use the same _wp_page_template meta to store the template as pages.

Here are some arguments that should work:

$args = array(
    'post_type' => 'post',
    'meta_key' => '_wp_page_template',
    'meta_value' => 'templatename.php'
);

If your template is within a folder in your theme, the path will need to be included as well:

$args = array(
    'post_type' => 'post',
    'meta_key' => '_wp_page_template',
    'meta_value' => 'templates/templatename.php'
);

You can also query for posts that use the default template:

$args = array(
    'post_type' => 'post',
    'meta_key' => '_wp_page_template',
    'meta_value' => 'default'
);
0

Thanks all for the responses. It turns out the site in question was actually using the "single post template" plugin, and the templates were defined like this:

Single Post Template: Fullwidth
Description: Like single post but without sidebar.

I think this was in place for a while. I updated the templates to this:

Template Name: Fullwidth
Template Post Type: post
Description: Like single post but without sidebar.

I got rid of the extra plugin, reassigned the posts, and now my original query is working.

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.