Site icon Hip-Hop Website Design and Development

Customized rewrite rule based mostly on different customized put up sort title

I’m attempting to alter url of 1 customized posts sort based mostly on title of the opposite one. To realize this.

first customized put up /test1/aaa

the opposite customized posts aftere rewrite

/test1/aaa/123

/test1/aaa/456

/test1/aaa/785

and so forth.

These customized posts are related with ACF Object area solely.

So what I acquired thus far and it nearly works is

perform apartments_list() {
    $labels = array(
        'title'               => _x( 'Mieszkanie', 'put up sort basic title' ),
        'singular_name'      => _x( 'Mieszkania', 'put up sort singular title' ),
        'add_new'            => _x( 'Dodaj', 'kategoria' ),
        'add_new_item'       => __( 'Dodaj' ),
        'edit_item'          => __( 'Edytuj' ),
        'new_item'           => __( 'Nowy ingredient' ),
        'all_items'          => __( 'Wszystkie elementy' ),
        'view_item'          => __( 'Zobacz ingredient' ),
        'search_items'       => __( 'Szukaj' ),
        'not_found'          => __( 'Brak elementów' ),
        'not_found_in_trash' => __( 'Elementy w koszu' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Mieszkania'
    );
    
    $args = array(
        'labels'        => $labels,
        'description'   => 'Mieszkania',
        'public'        => true,
        'menu_position' => 20,
        'show_ui'       => true,
        'show_in_rest'  => true,
        'show_in_admin_bar'=> true,
        'hierarchical' => false,
        //'has_archive' => true,
        'menu_icon'     => 'dashicons-editor-kitchensink',
        'helps'      => array('title', 'page-attributes'),
        'rewrite' => array(
            'with_front' => false,
            'slug' => '/inwestycja/%mieszkanie_slug%'
            //'slug' => '/inwestycja/lofthaus-zablocie-test'
        )
    );
    register_post_type( 'mieszkanie', $args );  
}
add_action( 'init', 'apartments_list' );


//Rewrite rule
perform change_apartment_url( $url, $put up ) {
    if ( 'mieszkanie' == get_post_type( $put up ) ) {
        $inwestycja = get_field('inwestycja');
        $inwestycjaObj = get_post($inwestycja);
        //$inwest_slug = 'lofthaus-zablocie-test';
        $inwest_slug = $inwestycjaObj->post_name;
        return str_replace('%mieszkanie_slug%', $inwest_slug, $url);
    }
    return $url;
}
add_filter( 'post_type_link', 'change_apartment_url', 10, 2 );

and this for altering residences title to id solely

//Change residence slug to put up ID
perform slug_save_post_callback( $post_ID, $put up, $replace ) {
    // permit 'publish', 'draft', 'future'
    if ($post->post_type == 'mieszkanie') {
            
        // solely change slug when the put up is created (each dates are equal)
        if ($post->post_date_gmt != $post->post_modified_gmt)
            return;

        $new_slug = sanitize_title( $post->id, $post_ID );
        
        //$new_slug .= '-' . $subtitle;
        if ($new_slug == $post->id)
            return; // already set
        
        // unhook this perform to stop infinite looping
        remove_action( 'save_post', 'slug_save_post_callback', 10, 3 );
        // replace the put up slug (WP handles distinctive put up slug)
        wp_update_post( array(
            'ID' => $post_ID,
            'post_name' => $new_slug
        ));
        
        // re-hook this perform
        add_action( 'save_post', 'slug_save_post_callback', 10, 3 );
        
    }
}
add_action( 'save_post', 'slug_save_post_callback', 10, 3 );

So with that code the opposite customs put up’s url is being modified positive, whereas saving the put up.
However then once I open that url, it redirects to the primary customized put up.

Solely once I change slug to fastened title and save perma hyperlinks to clear guidelines, it really works. However I would like it to be dynamic. Any concept ?

'rewrite' => array(
            'with_front' => false,
            //'slug' => '/funding/%mieszkanie_slug%'
            'slug' => '/funding/lofthouse'
        )