0

I am having problem when I want to display my taxonomy template.
I have created one custom taxonomy 'Location' for my custom post type 'Project'. Moreover, I have created templates for it as :

taxonomy-{taxonomy_name}-{term}.php
taxonomy-{taxonomy_name}.php
taxonomy.php

None of them work... I just get my archive-{post_type}.php in all case.

My targetted URLs are :
mywebsite.com/holliday/united-kingdom/ (Location taxonomy)
mywebsite.com/holliday/united-kingdom/london (Location taxonomy child)

I should have messed up or missed something when creating my taxonomy but can't get it right.

Here is my code (register post type and register taxonomy) :

// Register Post Type (Holliday)
$capability = 'publish_posts';

$cpt_args = array(
    'menu_icon'         => 'dashicons-groups',
    'labels'            => array(
        'name'              => __( 'Holliday', 'mon-plugin' ),
        'singular_name'     => __( 'Holliday', 'mon-plugin' ),
        'add_new'           => __( 'Add Holliday', 'mon-plugin' ),
        'add_new_item'      => __( 'Add Holliday', 'mon-plugin' ),
        'edit'              => __( 'Edit', 'mon-plugin' ),
        'edit_item'         => __( 'Edit Holliday', 'mon-plugin' ),
        'new_item'          => __( 'New Holliday', 'mon-plugin' ),
        'view'              => __( 'View Holliday', 'mon-plugin' ),
        'view_item'         => __( 'View Holliday', 'mon-plugin' ),
        'search_items'      => __( 'Search Holliday', 'mon-plugin' ),
        'not_found'         => __( 'No Holliday found', 'mon-plugin' ),
        'not_found_in_trash'=> __( 'No Holliday found in Trash', 'mon-plugin' ),
        'parent'            => __( 'Parent Holliday', 'mon-plugin' ),
    ),
    'public'            => true,
    'has_archive'       => true,
    'rewrite'           => array(
        'slug'              => 'holliday/%my_project_location%',
    ),
    'supports'          => array('title', 'custom-fields', 'excerpt', 'editor', 'author', 'thumbnail', 'comments'),
    'capabilities'      => array(
        'publish_posts'     => $capability,
        'edit_posts'        => $capability,
        'edit_others_posts' => $capability,
        'delete_posts'      => $capability,
        'delete_others_posts'=> $capability,
        'read_private_posts'=> $capability,
        'edit_post'         => $capability,
        'delete_post'       => $capability,
        'read_post'         => $capability
    ),
);
register_post_type( 'my_project', $cpt_args );

// Register Taxonomy (Location)
$cats_args = array( 
    'labels'            => array(
        'name'              => _x( 'Project Location', 'mon-plugin' ),
        'singular_name'     => _x( 'Location', 'mon-plugin' ),
        'search_items'      => __( 'Search Location', 'mon-plugin' ),
        'all_items'         => __( 'All Location', 'mon-plugin' ),
        'parent_item'       => __( 'Parent Location', 'mon-plugin' ),
        'parent_item_colon' => __( 'Parent Location:', 'mon-plugin' ),
        'edit_item'         => __( 'Edit Location', 'mon-plugin' ),
        'update_item'       => __( 'Update Location', 'mon-plugin' ),
        'add_new_item'      => __( 'Add New Location', 'mon-plugin' ),
        'new_item_name'     => __( 'New Location Name', 'mon-plugin' ),
        'menu_name'         => __( 'Location', 'mon-plugin' ),
    ),
    'hierarchical'      => true,
    'public'            => true,
    'show_ui'           => true,
    'has_archive'       => true,
    'rewrite'           => array(
        'slug'              => 'holliday',
        'hierarchical'      => true
    )
);
register_taxonomy( 'my_project_location', 'my_project', $cats_args );

EDIT:
If I change my taxonomy slug into something else, I get my taxonomy template working.
Example:

 taxonomy slug => "location"  
 mywebsite.com/location/united-kingdom/ => Taxonomy template working !  

But I want my taxonomy to work with my custom post type. To get my taxonomy template when accessing mywebsite.com/{custom-post-type}/{taxonomy}/
But this give me archive-{custom-post-type}.php file instead.

Following the Template Hierarchy I expect Wordpress to find me the taxonomy template before the archive template.

Is there something I am missing ?
Thank you for your help.

8
  • Do you see your taxonomy in admin page? I you really sure that it was loaded? Commented Aug 22, 2017 at 17:44
  • Can you show the code used to register your CPT, as well as the URL you are accessing that is ending up using archive-{post_type}.php? Also, have you visited the permalink settings page since registering the CPT and tax?
    – WebElaine
    Commented Aug 22, 2017 at 17:44
  • @AntonLukin, Yes I see it, no problem to access my custom post. Commented Aug 23, 2017 at 13:07
  • @WebElaine, I visit my permalink page each time I change the code to register my taxonomy. Commented Aug 23, 2017 at 13:07
  • 3
    It looks like you are registering both your CPT and your Taxonomy to use the /holliday/project-location/ URL structure. Whenever there are 2 content types at the same URL, only one of them will win out. You could try unregister_taxonomy and then re-register it with a totally different URL just to test out whether or not this is the case, then if so, find a new permalink plan.
    – WebElaine
    Commented Aug 23, 2017 at 14:49

2 Answers 2

1

There are a couple of different solutions if you want to have only the /holliday/project-location/ URL and not separate archive and taxonomy URLs.

Option 1:

Use the template_include() filter. I think this code would work but have not tested:

add_filter( 'template_include', 'holliday_template', 99 );

function holliday_template( $template ) {

    if ( is_post_type_archive( 'holliday' )  ) {
        $new_template = locate_template( array( 'taxonomy-my_project_location.php' ) );
        if ( '' != $new_template ) {
            return $new_template;
        }
    }

    return $template;
}

Option 2 (even simpler):

Tell WP not to give your holliday CPT an archive. When you register the post type, just change 'has_archive' => true, to false. That way, when WP looks for content at your desired URL, only the taxonomy archive exists. You will most likely need to use unregister_post_type() and re-register it to force the change.

0

Thank you WebElaine for your help ! I figured out a way to solve it.

It looks a bit as you proposed in Option 2. Except that I used the filter parse_query to change my CPT 'has_archive' to false when accessing my taxonomy.

Here is the code :

function check_taxonomy_query( $query ) {
    if ($query->query['my_project_location'] && !$query->query['my_project']) {
        $query->is_post_type_archive = false;
    }
}
add_action( 'parse_query', 'check_taxonomy_query' );  

my_project_location and my_project are the query vars used for my rewrite rules:

$newRules['holliday/?$']    =   'index.php?lang=en&post_type=my_project';
$newRules['holliday/([^/]+)/?$']    =   'index.php?lang=en&post_type=my_project&my_project_location=$matches[1]';
$newRules['holliday/([^/]+)/([^/]+)/?$']    =   'index.php?lang=en&post_type=my_project&my_project_location=$matches[1]/$matches[2]';
$newRules['holliday/([^/]+)/([^/]+)/([^/]+)/?$']    =   'index.php?lang=en&post_type=my_project&my_project_location=$matches[1]/$matches[2]&my_project=$matches[3]';

So in the check_taxonomy_query function, I simply check if I am in the case of the 2nd or 3rd line of my rewrite rules. If yes, I turn the post type archive down.

Hope it could help someone ;)

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.