I’m having a small issue with category pages for a custom taxonomy when custom permalinks are used, and I was wondering if someone might be able to help out and point out where I’m going wrong.
I’ve created a taxonomy “Brands” for use with a custom post type (in this case, Products), using the code below place in the functions.php file of the theme. I’ve created a page template for the Brands page, which lists all the brands using the wp_list_categories($args) template tag, and that’s all fine.
Now, if I use WordPress’ default permalink settings (where you get urls ending with ?page_id=7 and similar), clicking on any of those brands in the list, takes you to a category page which lists all the products for that brand – which is exactly the intent. If however, I set any other custom permalink structure (say /xyz/%postname%/ ), when you click on the brand name I get the 404 page instead of the category page (the same also happens when clicking view on a Brand when viewing the list within the WordPress admin).
With the custom permalink structure, when clicking on the brand it appears to be using the correct url structure (http://www.siteroot.com/brands/brandname/) when showing the 404 page. With the default permalink setting it goes to http://www.siteroot.com/?brand=brandname, which works correctly.
I’ve checked various guides on making custom taxonomies, and have tried to be very careful with setting the rewrite for the taxonomy, and now I’m at a loss as to what the issue is. Any help will be much appreciated.
add_action( 'init', 'build_taxonomies', 0 );
function build_taxonomies() {
register_taxonomy(
'brand',
'products',
array(
'labels' => array(
'name' => 'Brands',
'singular_name' => 'Brand',
'search_items' => 'Search Brands',
'popular_items' => 'Popular Brands',
'all_items' => 'All Brands',
'parent_item' => 'Parent Brand',
'parent_item_colon' => 'Parent Brand:',
'edit_item' => 'Edit Brand',
'update_item' => 'Update Brand',
'add_new_item' => 'Add New Brand',
'new_item_name' => 'New Brand Name'
),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'sort' => true,
'query_var' => true,
'args' => array('orderby' => 'term_order'),
'rewrite' => array('slug' => 'brands', 'with_front' => false)
)
);
}
Add
flush_rewrite_rules();
after your register taxonomies. That fixed it for me.Edit
Do make sure you do it on install of your plugin if you’re using it on one. It should only be run once on pug-in activation – and not on every page load. Alternatively visit Settings > Permalinks page as this flushes the rules too.
If you use a permalinkg structure with a front base, make sure
with_front
istrue
.I may be way off here (and this is an old question), but are you by chance using the WP No Category Base plugin? or have you in the past on this site?
I found that when I disabled the plugin, I would get similar results to the ones you are describing.
I also had an issue with a single category listing “no posts found”.
I never figured out what caused the problem, but I fixed it. My method is posted on the WordPress.org forums.
What finally worked for me was changing a value when defining the taxonomy properties.
If you set
'rewrite' => false,
then it should not give you any more problems, and you’ll even be able to set the permalinks to whatever you want without giving any troubles to you custom taxonomy 🙂