Custom posts with the same slime. Redirecting to the wrong post with the same slug

I have 2 user messages like Post.The Post Post and City-Guide Post. The first post (video message) contains the URL: 104.130.239.132/rick-owens/ , and the second mail (City-Guide Post) encodes the url: http://104.130.239.132/city-guide/rick-owens/ (a guide city is a permalink that is the name of a personalized message type). The SO problem occurs here whenever we try to access the first URL, it displays the pattern and contents of the second URL. The second URL is published last. I tried to decide on my own by disabling the Yoast Seo plugin, still not changed, and also made my permalink the updated ones still got the same results.

Attaching a Yoast SEO snippet to the first URL: enter image description here and Yoast SEO fragment of the second url message enter image description here Any help would 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.Gity 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 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; } 
+7
redirect php wordpress permalinks custom-post-type
source share
3 answers

This problem is very common, since the name of the drain of two types of messages similarly, the one that is last will be displayed. Now it does not reflect the type of message. I assume that you have chosen "post-name" or% postname% in the permalink structure. To solve the problem, the first and easiest way is to modify a message from a single message.

Another is to change the permalink structure of the post type from functions.php by overwriting the functions of the WordPress class. To do this, you need to see the link below the message header. This is due to the fact that to find out the bullet of the mail type of a specific user-defined message type.

I executed your provided code in my local installation. Now I have 2 custom message types, both yours and the mail in both โ€œrick owensโ€ names, in which slug will be โ€œrick-owensโ€. Firstly, the same error is displayed as mentioned.

I solved this problem by adding this function to 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'); 

Worked on this code for me and was tested and should work for you too.

As I mentioned, you should know the URL of the message from the edit page of this particular message.

For a custom video ad type, the mail URL should look like this:

www.siteurl.com/rewrite/rick-owens

For the type of mail pointer, the mail URL should look like this:

www.siteurl.com/city-guide/rick-owens

If I'm right, the same link structure should also be on your page, so the code should work as I provided at the top. If you have links other than the ones mentioned above, change them in the functions.php code that I provided. For example, if you have for a type of personalized video,

www.siteurl.com/video/rick-owens

Then change the functions.php code to

 $structure = 'video/%rewrite%'; 

see img for checking video url

If you have a special type of mail,

www.siteurl.com/city_guide/rick-owens

 $structure1 = 'city_guide/%rewrite%'; 

see img to check city url

This is another option that I usually do if any conflict happens the same as yours in my project. If you do not have one, please write to me about it.

Hope this works.

Updated answer as from your updated process

Please use this code to upgrade your message type to 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 works for you and please delete the code as I mentioned above and remember to update the permalink.

thanks

NOTE. Remember to update the permalink after you put the code in functions.php

+1
source share

Update the code for the type of mail message with the code below and, if possible, use the same code for the type of the city's mailing address

 $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 ); 
+2
source share

in setting up Wordpress permalinks, put a custom structure, for example /% category% /% postname% /, then in your first interview you will notice the yoast seo gear tab? you can put / rick-owens / as the canonical URL.

-one
source share

All Articles