0

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',
                ),
            ) );
        }
    }
6
  • your taxonomy is hierarchical, so it's generating rules for multiple levels of terms that clash with the post permalinks.
    – Milo
    Commented Mar 22, 2016 at 16:07
  • @Milo Changing the hierarchical flag to false for the post arguments hasn't solved the issue, with a permalink flush to make sure
    – Solflux
    Commented Mar 22, 2016 at 16:12
  • For the post arguments? Do you mean taxonomy? Look at the query vars and SQL query for each request and you will see what WordPress is trying to query for.
    – Milo
    Commented Mar 22, 2016 at 16:28
  • My mistake, I did mean Taxonomy... The issue is going to something like /cosmetics/treatments/body/waxing presents a 404, so where would I be checking the query vars?
    – Solflux
    Commented Mar 22, 2016 at 16:43
  • $wp_query contains the query vars and SQL after the query is run.
    – Milo
    Commented Mar 22, 2016 at 16:49

1 Answer 1

0

Your previous edit was a bit closer. Change the post type rewrite slug to:

cosmetics/treatments/%treatment-type%

Then change all instances of %type% to %treatment-type% in the treatment_permalink function.

Everything should then work correctly after you flush rewrites.

For future reference, the Monkeyman rewrite analyzer plugin is very helpful for debugging rewrite rules. As I mentioned, you can also dump the contents of $wp_query to see what WordPress is trying to query for in each request. You can put something like this directly in a 404 template for quick debugging of requests that you think should not be a 404:

<pre>
<?php print_r( $wp_query ); ?>
</pre>

You'll see a list of all query vars with their values and the resultant SQL query WordPress is sending to the database.

1
  • Thank you! This has finally done it, sorry I didn't understand you meant to print the query vars on the the 404 page. I'll know for next time
    – Solflux
    Commented Mar 24, 2016 at 12:53

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.