johnbillion / extended-cpts

A library which provides extended functionality to WordPress custom post types and taxonomies.
GNU General Public License v2.0
979 stars 96 forks source link

Rewrite rules on cpt issue with multiple rewrites #81

Closed mattpirelli closed 6 years ago

mattpirelli commented 7 years ago

I have a cpt defined this way (skipping the irrelevant parts):

register_extended_post_type( 'course', [ 'supports' => 'title', 'rewrite' => array ( 'permastruct' => '/%course_type%/%course%/' ) ]);

"course_type" is a custom taxonomy name

On the WP backend, permalink structure is set as /%postname%/

Visiting every single page of a course works as expected, e.g. domain.dev/shoe-courses/postname, but the moment I visit either a page, a post or a cpt, i get a bizarre error (e.g. visiting domain.dev/testimonials/testimonial_name) as shown in the attached file screen shot 2017-07-20 at 15 41 55

Digging trough the queries argument, I notice this:

page=&category_name=testimonials&course=testimonial_name&post_type=course&name=testimonial_name

Seems like the rewrite for the CPT "Courses" is being applied to every content of the website. I'm on PHP7 and Nginx, if that helps to check some config files

Any idea on what's going on?

johnbillion commented 6 years ago

This appears to be an issue with the priority of rewrite rules for taxonomies. They take precedence over other rewrite types, which causes WordPress to first look for a term in the course_type taxonomy with the slug in the URL.

I think it's outside the scope of this library to attempt to avoid this issue, and once you start messing with rewrite rule priorities you need to really understand what's going on to avoid messing things up.

I would recommend switching to an alternative permalink structure for either this post type or for the general permalink structure to avoid this issue.

If you don't want to do that, the following three plugins are worth trying out if you want to get around this issue: