0

I have a situation where it is needed that if I create two CPT pages wich both have the same name and the same slug.

For example, if I add two CPT pages, wich both called "Post Name":
The first slug will be "post-name",
The second slug will be "post-name-2".

Because I added a custom part to the permalink structure wich will make the URL unique:

function extra_post_link( $permalink, $post, $leavename ) {
    global $wp_rewrite;

    if ( stripos( $permalink, '%selected_meta%' ) == false )
        return $permalink;

    if ( is_object( $post ) && ($post->post_type == 'ervaring' || $post->post_type == 'lening') ) {
        $_selected_post_slug = '';
        $_selected_post_id = get_post_meta( $post->ID, 'rating-post', true );

        if ( $_selected_post_id )
            $_selected_post_slug = get_page_uri( $_selected_post_id );

        return str_replace( '%selected_meta%', $_selected_post_slug, $permalink );
    }

    return $permalink;
}

add_filter( 'post_type_link', 'extra_post_link', 10, 3 );

How do I prevent WordPress from doing this?

1 Answer 1

2

you might consider the 'wp_unique_post_slug' filter:

   add_filter( 'wp_unique_post_slug','my_disable_unique_slug',11,6);
    function disable_unique_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ){
        global $post;       
        if($post->post_type=='cpt'){ // EDIT post type
            $slug=$original_slug;
        }
        return $slug;
    }

This is untested, but you get the idea ;-)

ps: you might have to change other things to make this work, since the unique-ness is so important.

1
  • Oh thank you very much! I sure have to change some other things in order to receive the correct post when the item is viewed. But since that wasn't exactly the question, I consider this one answered. :-) Commented Feb 6, 2013 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.