Hi,
After running a clean install, I was able to find a solution to issue.
Note: This is an issue that is linked to custom taxonomy for Wordpress and is not related to the routing on sage.
However, whilst looking online for a solution this has proven problematic for a lot of developers and for clarity I will explain the solution here.
First of all my goals are:
- To set up a custom post type (faqs). The posts should have a URL of
/faqs/[custom taxonomy]/postname
- To set up custom taxonomy as a listing page that has its own template. The URL should be
/faqs/[custom taxonomy]
To do this, you need to register your taxonomy before your custom post type.
register_taxonomy( 'faq-category', 'faq', array(
'public' => true,
'publicly_queryable' => true,
'hierarchical' => true,
'has_archive' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'faqs',
'with_front' => false,
),
));
Register the taxonomy name and ensure that the options public and public_queryable are set to true. Otherwise, your category page will redirect to the homepage. For the slug make this the base name for your custom post structure. In my case, this is faqs.
$labels = array(
'name' => _x( 'FAQs', 'FAQ', 'faq' ),
'singular_name' => _x( 'FAQs', 'FAQ', 'faq' ),
'name_admin_bar' => _x( 'FAQ', 'FAQ', 'faq' ),
'add_new' => _x( 'Add New', 'FAQ', 'faq' ),
'new_item' => __( 'New FAQ', 'faq' ),
'edit_item' => __( 'Edit FAQ', 'faq' ),
'view_item' => __( 'View FAQ', 'faq' ),
'all_items' => __( 'All FAQs', 'faq' ),
'search_items' => __( 'Search FAQs', 'faq' ),
'parent_item_colon' => __( 'Parent FAQs:', 'faq' ),
'not_found' => __( 'No FAQs found.', 'faq' ),
'not_found_in_trash' => __( 'No FAQs found in Trash.', 'faq' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'faq' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'faqs/%category%', 'with_front' => false ),
'capability_type' => 'post',
'has_archive' => 'faq-category',
'hierarchical' => false,
'menu_position' => 24,
'menu_icon' => 'dashicons-editor-help',
'supports' => array( 'title', 'excerpt' )
);
register_post_type( 'faq', $args );
When registering the custom post type, add /%category%
to the rewrite slug. This will be used in the following function to add the custom taxonomy to the permalink structure.
At this point, it is important to set with_front to false if in my case you have a permalink structure that is not /%postname%
(I also needed to facilitate a /blog/%postname%
permalink structure).
Lastly, set the has_archive option to the name of your custom taxonomy. this will be used for your custom taxonomy page. In my case, I have created taxonomy-faq-category.php
To rewrite the url to custom taxonomy permalink structure, I used this function:
// to add faq custom taxonomy to faq links - for later
function add_category_to_url( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'faq-category' );
if( $terms ){
return str_replace( '%category%' , $terms[0]->slug , $post_link );
}
if(!$terms && ($post->post_type == 'faq')) {
return str_replace( '%category%/' , '' , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'add_category_to_url', 1, 3 );
Additionally, make sure you save the permalinks under settings->permalinks as you make changes to your custom taxonomy or custom post type to ensure any url rewrites are updated.
The following links have been helpful:
Also last but not least many thanks to @craigpearson & @alwaysblank for offering their help with this issue.