How can I get this request to use the Custom Post Type page template instead?

I read the answer for this post which says you can do something like this:

http://example.com?post_type=car&color=red

When viewing $wp_query everything seems to be working fine. That is, I’m getting ‘car’ post types that have the ‘red’ term assigned. However, the request uses the ‘color’ custom taxonomy template. (In my case this is taxonomy.php.)

Read More

How can I get this request to use the ‘archive-car.php’ custom post type template instead?

Related posts

Leave a Reply

2 comments

  1. Switch the taxonomy.php just like you would the single.php to support specific post custom templates. This is untested code, so it may not work – but, first get the post type

    $post_type = get_query_var('post_type');
    

    So if $post_type contains post_type = ‘car’ then include archive-car.php like this:

    if ( $post_type = 'car' ) 
      get_template_part('archive-car');
    elseif ( $post_type = 'boat'  ) 
      get_template_part('archive-boat');
    elseif ( $post_type = 'bike' ) 
      get_template_part('archive-bike');
    else 
      get_template_part('regular-taxonomy');      
    

    An better alternative would be if you could establish a template redirect like in this answer, but I don’t know if that’s even possible. If possible, you’d redirect the taxonomy template based on the value of $post_type.

    How to quickly switch custom post type singular template?

  2. I’m not sure that’s possible. My understanding is that whenever you are viewing posts that are filtered by a taxonomy wordpress looks for taxonomy templates. Based on hierarchy you could get rid of any taxonomy pages and only have archive pages available as templates, but that seems a bit extreme and not too useful.

    What are you hoping to achieve with this code?

    You could build functions into your archive-car.php page to react to cars in a specific color term. Perhaps that will get you the functionality you’re looking for.