I have a custom post type documenting Treatments available from a clinic. This sits on the Cosmetics part of the website (site.com/cosmetics/
).
Filters have managed to translate \%type\
for permalinks. This is the structure I'm after:
site.com/cosmetics/treatments
site.com/cosmetics/treatments/[type]/
site.com/cosmetics/treatments/[type]/[treatment]
I've already tried to follow many of he answers on here. Any ideas or obvious flaws in my code? I've been flushing the permalinks each time already.
site.com/cosmetics/treatments/[type]
is working fine.
Treatment pages themselves link to the following from the admin panel:
site.com/[type]/[treatment]
This is a 404 error.
UPDATE
I've rearranged some of the code, so I've changed the code on here to reflect that. The problem remains unchanged.
Plugin Code:
class NSCTreatments {
/**
* Constructor for taxonomy and post type initialisation
*/
function __construct(){
add_action( 'init', array( $this, 'register_custom_taxonomies') ); // this MUST go first
add_action( 'init', array( $this, 'register_custom_post_type' ) );
add_filter('post_link', 'treatment_permalink', 10, 3);
add_filter('post_type_link', 'treatment_permalink', 10, 3);
function treatment_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%type%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'treatment-type');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'no-type';
return str_replace('%type%', $taxonomy_slug, $permalink);
}
}
function register_custom_taxonomies (){
$labels = array(
'name' => _x( 'Treatment Types', 'taxonomy general name' ),
'singular_name' => _x( 'Treatment Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Treatment Types' ),
'all_items' => __( 'All Treatment Types' ),
'parent_item' => __( 'Parent Treatment Types' ),
'parent_item_colon' => __( 'Parent Treatment Types:' ),
'edit_item' => __( 'Edit Treatment Type' ),
'update_item' => __( 'Update Treatment Type' ),
'add_new_item' => __( 'Add New Treatment Type' ),
'new_item_name' => __( 'New Treatment Type Name' ),
'menu_name' => __( 'Treatment Type' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => false,
'query_var' => true,
'rewrite' => array( 'slug' => 'cosmetics/treatments', 'with_front' => false ),
);
register_taxonomy( 'treatment-type', array( 'treatment' ), $args );
}
/**
* Registers a Custom Post Type called Treatment
*/
function register_custom_post_type() {
register_post_type( 'treatment', array(
'labels' => array(
'name' => _x( 'Treatments', 'post type general name', 'nsc-treatments' ),
'singular_name' => _x( 'Treatment', 'post type singular name', 'nsc-treatments' ),
'menu_name' => _x( 'Treatments', 'admin menu', 'nsc-treatments' ),
'name_admin_bar' => _x( 'Treatment', 'add new on admin bar', 'nsc-treatments' ),
'add_new' => _x( 'Add New', 'treatment', 'nsc-treatments' ),
'add_new_item' => __( 'Add New Treatment', 'nsc-treatments' ),
'new_item' => __( 'New Treatment', 'nsc-treatments' ),
'edit_item' => __( 'Edit Treatment', 'nsc-treatments' ),
'view_item' => __( 'View Treatment', 'nsc-treatments' ),
'all_items' => __( 'All Treatments', 'nsc-treatments' ),
'search_items' => __( 'Search Treatments', 'nsc-treatments' ),
'parent_item_colon' => __( 'Parent Treatment:', 'nsc-treatments' ),
'not_found' => __( 'No treatment found.', 'nsc-treatments' ),
'not_found_in_trash' => __( 'No treatment found in Trash.', 'nsc-treatments' ),
),
// Frontend
'has_archive' => 'cosmetics/treatments',
'public' => true,
'publicly_queryable' => true,
'rewrite' => array('slug' => '%type%', 'with_front' => false),
// Admin
'capability_type' => 'post',
'menu_icon' => 'dashicons-screenoptions',
'menu_position' => 20,
'query_var' => true,
'show_in_menu' => true,
'show_ui' => true,
'supports' => array(
'title',
'author',
),
) );
}
}
hierarchical
flag to false for the post arguments hasn't solved the issue, with a permalink flush to make sure/cosmetics/treatments/body/waxing
presents a 404, so where would I be checking the query vars?$wp_query
contains the query vars and SQL after the query is run.