My apologies. I used the wrong tags to identify the code snippets and there does not seem to be a way for me to edit the post to fix it. Here is the code again, I hope properly tagged:
add_action( 'init', 'my_register_post_types_product' );
add_action( 'init', 'my_register_post_types_project' );
This calls the two functions that create the custom post types. Here are the key sections of that code:
function my_register_post_types_product() {
$args = array(
...
'rewrite' => array(
'slug' => 'product', // string (defaults to the post type name)
'with_front' => false, // bool (defaults to TRUE)
'pages' => true, // bool (defaults to TRUE)
'feeds' => false, // bool (defaults to the 'has_archive' argument)
'ep_mask' => EP_PERMALINK, // const (defaults to EP_PERMALINK)
),
...
);
register_post_type(
'product', $args);}
And:
function my_register_post_types_project() {
$args = array(
...
'rewrite' => array(
'slug' => 'project', // string (defaults to the post type name)
'with_front' => false, // bool (defaults to TRUE)
'pages' => true, // bool (defaults to TRUE)
'feeds' => false, // bool (defaults to the 'has_archive' argument)
'ep_mask' => EP_PERMALINK, // const (defaults to EP_PERMALINK)
),
...
);
register_post_type(
'project', $args);}
I suspect you cannot have more than one post type where 'with_front'=>false. Without a front parameter in the permastruct, WP queries for the wrong post type with the requested post.
Thanks. Unfortunately that doesn’t solve the problem. I tried changing just one and also both to be 'with_front'=>true and the issue did not go away. After making that change I did go to the permalinks page and re-saved the settings there to flush the permalinks.
I seem to have found the solution. I had to explicitly set the query_var for both custom post types. This doesn’t really make sense to me as these should have been defaulting to the names of the custom post types, which is what I have now explicitly set these to be, but explicitly setting them solved the problem I was having.
Thank you, bcworkz, for your suggestion. While it wasn’t the right solution it prompted me to look carefully at the settings for each of the variables I was using, which lead me to the solution.
No problem. It’s funny how a wrong suggestion can sometimes lead to the right solution 🙂
While it’s not unusual in some cases to need to explicitly add CPTs to the query var, such as a category query, it shouldn’t be necessary for a single post query when the post type is part of the permalink. As long as you have it working now I guess it’s all good.