Multiple uri to same post in WordPress

I wanted to make two uri to point same post in WordPress but without redirect,
mysite.co.uk/awesome-post
mysite.co.uk/?p=12

I want those two uri to be pointed to same post but when you reach the page uri shouldn’t change.

Related posts

1 comment

  1. The only solution that i know is duplicate your post and get second link from it and hide the duplicate from loop and if you wish back end.

    for that you have to use save_post action and plugin/theme activation hook for duplicate posts that have already published (this can be init hook but with caution you need to do this only one time).

    first you have to loop through all posts and make duplicate

    add_action('switch_theme', 'your_prefix_setup_options');
    
    function your_prefix_setup_options () {
      // WP_Query arguments
        $args = array (
            'post_type'              => array( 'post' )
        );
    
        // The Query
        $query = new WP_Query( $args );
    
        // The Loop
        if ( $query->have_posts() ) {
    
            while ( $query->have_posts() ) {
                $query->the_post();
    
                // duplicate post like this
                your_prefix_post_duplicate( get_the_ID(), get_the_title(), get_the_content(), get_post_thumbnail_id() );
    
            }
    
        } else {
            // no posts found
        }
    
        // Restore original Post Data
        wp_reset_postdata();
    }
    
    function your_prefix_post_duplicate($post_id, $title, $content, $attachment){
    
    
        $post_id = $post_id;
        $post_name = $title;
        $content = $content;
        $attachment = $attachment;
    
    
        $slug = str_replace( " ", "-", $post_name );
        $slug = strtolower($slug);
    
        $post_data = array(
            'post_content' => $content,
            'comment_status'    =>  'closed',
            'ping_status'       =>  'closed',
            'post_author'       =>  0,
            'post_name'     =>  $slug,
            'post_title'        =>  $post_name,
            'post_status'       =>  'published',
            'post_type'     =>  'post'
        );
    
        $prefix = 'your_prefix__';
        //create your duplicate post
        $duplicate_post_id = wp_insert_post( $post_data );
        set_post_thumbnail( $duplicate_post_id, $attachment );
        add_post_meta( $duplicate_post_id, $prefix . 'duplicate', TRUE );
        //get duplicate link
        $perma_link = get_permalink ( $duplicate_post_id );
    
    
        //set this link to your post as meta
        add_post_meta( $post_id, $prefix . 'duplicate_url', $perma_link );
    
    }
    
    // now you can get second uri for fb like/share
    $second_link = get_post_meta(get_post_ID(),$prefix . 'duplicate_url', true);
    
    //and you can use the meta and ignore duplicate post from showing on loop 
    // or in advance you can use pre get post to hidethese duplicates from front end & back end permanently
    get_post_meta(get_post_ID(),$prefix . 'duplicate', true);
    

Comments are closed.