1

I have a custom post type registered as follows:

register_post_type( 'gs_business_listings',
    array(
      'labels' => array(
        'name' => __( 'Local Businesses' ),
        'singular_name' => __( 'Local Businesses' )
      ),
      'public' => true,
            'taxonomies'  => array( 'category', 'gs_business_listing_region' ),
      'has_archive' => true,
            'supports' => array('title', 'editor', 'thumbnail', 'revisions'),
            'register_meta_box_cb' => array($this,'add_meta_boxes'),
            'rewrite' => array('slug'=> apply_filters('goosesoft-coupons-archive-url', 'local-businesses'))
            )
  );

and for this custom post type I have a custom taxonomy which is registered as follows:

register_taxonomy(
            'gs_business_listing_region',
            'gs_business_listings',
            array(
                'labels' => array(
                    'name' => 'Regions',
                    'singular_name' => 'Region',
                ),
                'rewrite' => array( 'slug' => 'region', 'hierarchical' => true ),
                'hierarchical' => true,
                'has_archive' => true,
            )
        );

This works on the admin side. Everything is as expected. Regions shows in the Local Businesses submenu. I can save the Regions for a specific post.

But permalinks are not working for the taxonomies. What I need seems pretty straight forward and would be what I expect default behavior to be. I need the taxonomy url to be www.example.com/region/a-region where a-region is one of the taxonomies. But when I go to a url like that it does not work. More puzzling is that get_term_link() returns a url in exactly that format, but I get a 404 error on the very link that returns.

What could I be missing?

2
  • Did you flush rewrite rules after adding the taxonomy?
    – Milo
    Commented Dec 31, 2016 at 23:54
  • See my answer below. I was writing it when you posted. Thanks Commented Jan 1, 2017 at 0:00

1 Answer 1

2

There was nothing wrong with my code above. I found the answer at the bottom of the Taxonomies page in the Codex:

If your site uses custom permalinks, you will need to flush your permalink structure after making changes to your taxonomies, or else you may see a "Page Not Found" error. Your permalink structure is automatically flushed when you visit Settings > Permalinks in your WordPress dashboard.

In my plugin I already had flush_rewrite_rules(); being called upon deactivation, and I had deactivated and reactivated it several times. The fact that this didn't work really threw me for a loop.

But as soon as I visited Setting > Permalinks it all worked correctly!

3
  • You're right about my answer. It's pretty much a pointless answer so I've removed it. Probably shouldn't have been answering while distracted. You should flush the rewrite rules on plugin activation AND deactivation and AFTER your Post Types / Taxonomies have been registered. Easy way around it is to have a callback function to be called on init which registers your stuff. On plugin activation, call that init callback function and then immediately flush rewrite rules after. If you have a plugin settings page, you could also implement rewrite rule flushing when saving plugin settings page. Commented Jan 1, 2017 at 16:01
  • I was flushing on activation and deactivation. This says that calling this function on init is bad practice. codex.wordpress.org/Function_Reference/flush_rewrite_rules Commented Jan 1, 2017 at 19:49
  • You have a function which registers your Custom Post Types and Custom Taxonomies. Both are hooked on the action init. The register_activation_hook() function is called BEFORE the init hook is fired. You're flushing the rewrite rules before the the rewrite rules even exist. In the callback function which is triggered on plugin activation, make a call to your function which is used to register your Custom Post Types & Custom Taxonomies. Immediately after that, make a call to flush_rewrite_rules();. That way, your rewrite rules are created and WP is aware of them upon plugin activation. Commented Jan 1, 2017 at 19:56

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.