1

Hello I have a custom post type called results. I also created categories for that specific post type using a taxonomy. I'm not sure if I set it up correctly, but the code I have works so I'm sticking with it. If you see a better way or any errors please let me know.

I am able to create a custom post and set a category to it. Next I would like to create a categories page that will act like the regular archive.php but just for the category of the custom post types.

So say I have a custom post for results and I have its category set to car accidents I would like a way to display them all just like archive.php does for normal posts.

I tried going to a url like this but I get sent to a 404 page, even though I have an archive-results.php

www.myurl.com/results/categories/car-accidents

Here is the code I used to set up the custom post type and the taxonomy. Sorry If its long but I feel like its necessary to include everything.

// Create custom post type
function create_posttype() {
    register_post_type( 'Results',
        array(
            'labels' => array(
                'name' => __( 'Results' ),
                'singular_name' => __( 'Results' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'results'),
            'taxonomies'  => array( 'results', 'result-category' ),
        )
    );
}
add_action( 'init', 'create_posttype' );

//Create category for specific post type
function tr_create_my_taxonomy() {
    register_taxonomy(
        'results-categories',
        'results',
        array(
            'label' => __( 'Result Categories' ),
            'rewrite' => array( 'slug' => 'result-category' ),
            'hierarchical' => true,
            'has_archive' => true
        )
    );
}
add_action( 'init', 'tr_create_my_taxonomy' );

Am I missing something that is preventing this url from working?

www.myurl.com/results/categories/car-accidents

Thanks in advance

2
  • you set it to result-category where you registered the taxonomy, change that to results/categories
    – Milo
    Aug 4, 2017 at 17:19
  • in the first or second functin?
    – cup_of
    Aug 4, 2017 at 17:32

1 Answer 1

5

Your desired URLs:

www.example.com/results/post-name/
www.example.com/results/categories/category-name/

Two changes from your original code:

  1. Priority was added to both add_action hooks to reverse the order they execute. Order matters in this case, because the post type's attachment rewrite rules override the taxonomy rewrites. Side-effect: attachment URLs don't work for this post type!

  2. Rewrite for the taxonomy was changed to results/categories.


// Create custom post type
function create_posttype() {
    register_post_type( 'results',
        array(
            'labels' => array(
                'name' => __( 'Results' ),
                'singular_name' => __( 'Results' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'results'),
            'taxonomies'  => array( 'results', 'result-category' ),
        )
    );
}
// ADDED PRIORITY
add_action( 'init', 'create_posttype', 9 );

//Create category for specific post type
function tr_create_my_taxonomy() {
    register_taxonomy(
        'results-categories',
        'results',
        array(
            'label' => __( 'Result Categories' ),
            // CHANGED SLUG
            'rewrite' => array( 'slug' => 'results/categories' ),
            'hierarchical' => true,
            'has_archive' => true
        )
    );
}
// ADDED PRIORITY
add_action( 'init', 'tr_create_my_taxonomy', 8 );

Also note that the most specific general template for this will be taxonomy-results-categories.php, which will fall back to taxonomy.php, then archive.php.

0

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.