WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does anybody know of a way to modify the wordpress template process so that a single custom post type can be displayed on two separate templates based on the value of a custom field.

For example I have a single Custom Post Type called "plans"

It is using the single-plans.php and archive-plans.php files as templates by default.

What I would like to do is use this bit of code to filter out some of the results within my loop.

<?php //Filter the results based on the presence of $var
   $var = get_post_meta($post->ID, 'my_custom_field', true);
    if($var !== '') { ?>
      THE LOOP
    <?php } ?>

This works fine but i need create two separate templates where I can test for separate $var so I can have two menu items that each call on a separate version of the template and get a unique set of results from my single CPT

Thanks, Eric

share|improve this question
up vote 0 down vote accepted

Might be side-stepping your question, but can you use get_template_part?


    get_template_part( 'plans', get_post_meta($post->ID, 'my_custom_field', true ) ); 

Then create "plans_plana.php" and "plans_planb.php" (assuming that 'my_custom_field' can have values of 'plana' and 'planb').

twentyten and twentyeleven make heavy use of get_template_part if you need some more examples.

share|improve this answer
Great thought Jer I am probably trying to create a function that already exists in wordpress. I will read up on this and let you know if it solves my problem. – Zeusofolus Mar 1 '13 at 19:06
Just getting back to let you know it is working great. Thanks so much for opening up my mind to another great feature in WordPress. – Zeusofolus Mar 1 '13 at 22:00

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.