I have 2 Custom Post type posts.The video Post and City-guide Post The first Post(Video Post) contains the url: 104.130.239.132/rick-owens/ and the second Post(City-Guide Post) conatins url:http://104.130.239.132/city-guide/rick-owens/ (city-guide is the permalink structured which is the name of custom post type). SO the issue arrise here whenever we try to access the first url it displays the template and content of 2nd url.The 2nd url is latest published.I tried from myself to solve by disabling the Yoast Seo Plugin still no change and also done my permalink refreshed still got same results.
Attaching the Yoast SEO snippet for first url post: and the Yoast SEO snippet of second url post Any Help will be appreciated,Thanks.
Addition:
Here are my CPT codes.
1.Video Posts :
$labels = array(
'name' => _x( 'Videos', 'Post Type General Name', 'roots' ),
'singular_name' => _x( 'Video', 'Post Type Singular Name', 'roots' ),
'menu_name' => __( 'Video Posts', 'roots' ),
'parent_item_colon' => __( 'Parent Video:', 'roots' ),
'all_items' => __( 'All Videos', 'roots' ),
'view_item' => __( 'View Video', 'roots' ),
'add_new_item' => __( 'Add New Video', 'roots' ),
'add_new' => __( 'Add New', 'roots' ),
'edit_item' => __( 'Edit Video', 'roots' ),
'update_item' => __( 'Update Video', 'roots' ),
'search_items' => __( 'Search Video', 'roots' ),
'not_found' => __( 'Not found', 'roots' ),
'not_found_in_trash' => __( 'Not found in Trash', 'roots' ),
);
$rewrite = array(
'slug' => 'rewrite',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'video', 'roots' ),
'description' => __( 'Videos Post Type', 'roots' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'post',
'yarpp_support' => TRUE
);
register_post_type( 'video', $args );
2.City Guide Posts :
function register_post_types(){
register_post_type( 'city-guide', [
'has_archive' => TRUE,
'hierarchical' => TRUE,
'labels' => [
'name' => 'City Guide'
],
'public' => TRUE,
'supports' => ['editor', 'page-attributes', 'revisions', 'thumbnail', 'title','custom-fields','excerpt'],
'taxonomies' => array('post_tag')
] );
add_image_size( 'ipad-city-thumb', 650, 650, TRUE );
}
add_action( 'init', __NAMESPACE__.'\register_post_types', 20 );
I have this then in my code
remove /rewrite/ slug from custom permalinks, to allow domain/slug for all post types
public function post_link_rewrite( $post_link, $post, $leavename ){
$post_link = str_replace( '/rewrite/', '/', $post_link );
return $post_link;
}
Update register post type code with below code,and if possible use same code for city guide post type
$labels = array(
'name' => _x( 'Videos', 'Post Type General Name', 'roots' ),
'singular_name' => _x( 'Video', 'Post Type Singular Name', 'roots' ),
'menu_name' => __( 'Video Posts', 'roots' ),
'parent_item_colon' => __( 'Parent Video:', 'roots' ),
'all_items' => __( 'All Videos', 'roots' ),
'view_item' => __( 'View Video', 'roots' ),
'add_new_item' => __( 'Add New Video', 'roots' ),
'add_new' => __( 'Add New', 'roots' ),
'edit_item' => __( 'Edit Video', 'roots' ),
'update_item' => __( 'Update Video', 'roots' ),
'search_items' => __( 'Search Video', 'roots' ),
'not_found' => __( 'Not found', 'roots' ),
'not_found_in_trash' => __( 'Not found in Trash', 'roots' ),
);
$args = array(
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'yarpp_support' => TRUE
);
register_post_type( 'video', $args );
This issue is really common, since the slug name of the two post type is similar, the one which is latest one will be displayed. It doesn't reflect to the post type now. I assume you have chosen "post-name" or %postname% in a permalink structure. To solve the issue, the first and easiest way is to change the post slug of one of the posts.
Another is to change the permalink structure of the post type from the functions.php through rewrite class functions of WordPress. To do so, you have to look at the link below the post title. This is because to find out the slug of the post type of the particular custom post type.
I have implemented your provided code in my local installation. Now i have 2 custom post type as of yours and the post in both names "rick owens" which slug will be "rick-owens". Firstly, the same error is displayed as of have mentioned.
I solved this problem by adding this function in functions.php
function add_rewrite_rules_custom_post(){
global $wp_rewrite;
$structure = 'rewrite/%rewrite%';
$structure1 = 'city-guide/%city-guide%';
$wp_rewrite->add_permastruct('%rewrite%', $structure, false);
$wp_rewrite->add_permastruct('%city-guide%', $structure1, false);
}
add_action('init', 'add_rewrite_rules_custom_post');
Above this code worked for me and tested and should work for you also.
As i have already mentioned, you should know the post url from the edit page of that particular post.
For video custom post type, the post url should be like this:
www.siteurl.com/rewrite/rick-owens
For city guide post type, the post url should be like this:
www.siteurl.com/city-guide/rick-owens
If I am correct, the same link structure should be in your page too, so the code should work which I provided at the top. If you have different than that i have mentioned link above, then change according to it in the functions.php code that i have provided. For example, If you have for video custom post type,
www.siteurl.com/video/rick-owens
Then change the functions.php code to
$structure = 'video/%rewrite%';
see img for video url check
If you have for city guide custom post type,
www.siteurl.com/city_guide/rick-owens
$structure1 = 'city_guide/%rewrite%';
see img for city-guide url check
This is the another option, which i usually do if any conflict occur as similar as yours in my project. If you didn't got it please mention me on this post.
Hope this will work.
Updated Answer as from your updated process
Please use this code for updating your post type to the Wordpress Core functionality
function update_parse_request( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'page', 'video', 'rewrite' ) );
}
}
add_action( 'pre_get_posts', 'update_parse_request' );
Hope this code will work for you and please remove the code as i mention earlier above and dont forget to update the permalink.
Thank You
NOTE: Don't forget to update you permalink once you placed the code in functions.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With