I have tried this:


$query = new WP_Query(
  'post_type' => 'my-post-type',
  'post_parent' => $parent_id

while ($query->have_posts()) : $query->the_post(); 
if (is_page_template('my-template.php')) {
    echo 'is my template';


My template is in the theme's root and so is the template where I am placing the code.

  • What are you actually trying to check? The custom template used by the current post in the loop? Dec 6, 2021 at 14:52
  • Yes that's right, I want to loop through posts but only those using a certain template.
    – flinch85
    Dec 6, 2021 at 14:58

1 Answer 1


I managed to get the required result like this:

  $template = get_page_template_slug();

  if ($template === 'my-template.php') { 
   //do stuff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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