The problem is that the rewrite rules overlap, WordPress is trying to find a category name that matches the post slug.
A possible solution is to do a little manual query parsing, check if the request contains category_name
and is a term first, and reset query vars to post name
if nothing is found:
function wpd_fix_category_requests( $request ){
if( array_key_exists( 'category_name' , $request )
&& ! get_term_by( 'slug', basename( $request['category_name'] ), 'category' ) ){
$request['name'] = basename( $request['category_name'] );
unset( $request['category_name'] );
}
return $request;
}
add_filter( 'request', 'wpd_fix_category_requests' );
This is pretty basic, and maybe not reliable enough for primetime. Obviously you can’t have a post slug that matches a category name. Also, singular post pagination won’t work, and I assume attachment URLs will be broken as well.
You could fix all of those things following this method, if you were so inclined. Look at what $request
contains for each type of page view, maybe with a different permalink structure so you can see what it looks like in a successful case. You just need to convert the query vars from one kind of request to the other, and WordPress will carry on loading the correct object.