WordPress one custom post permalink structure inside other custom post permalink Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of WordPress one custom post permalink structure inside other custom post permalink without wasting too much if your time.

The question is published on by Tutorial Guruji team.

It’s a Membership website.

Listing (Custom Post Type - 1)

Team Member (Custom Post Type - 2)

When showing a single post for a listing

Then showing below all Team members for this listing.

But when I click a Team Member for a single post.

Then show WordPress Normal Permalink like this

http://exampol.com/team-member ( custom post type -2 ) / single post link/

Normal WordPress permalink for listing.

http://exampol.com/ listing ( custom post type -1 ) / single post link/

I want to show this type(Below) of permalink. Can it possible?

http://exampol.com/ listing ( custom post type -1 ) / single-post-link//team-member ( custom post type -2 ) / single-team-member-link/

Answer

I’ve conducted some research and found out this cool topic which I believe it will help you. https://1fix.io/blog/2016/02/05/parent-from-another-cpt/

Basically, and I’m quoting from the article, you need to create the CPTs:

    <?php
// Register Custom Post Types
function my_register_cpt() {
    $labels = array(
        'name'                  => _x( 'Courses', 'Post Type General Name', '1fix' ),
        'singular_name'         => _x( 'Course', 'Post Type Singular Name', '1fix' ),
        'menu_name'             => __( 'Courses', '1fix' ),
        'name_admin_bar'        => __( 'Courses', '1fix' )
    );
    $args = array(
        'label'                 => __( 'Course', '1fix' ),
        'labels'                => $labels,
        'hierarchical'          => true,
        'public'                => true
    );
    register_post_type( 'course', $args );
    $labels = array(
        'name'                  => _x( 'Lessons', 'Post Type General Name', '1fix' ),
        'singular_name'         => _x( 'Lesson', 'Post Type Singular Name', '1fix' ),
        'menu_name'             => __( 'Lessons', '1fix' ),
        'name_admin_bar'        => __( 'Lessons', '1fix' )
    );
    $args = array(
        'label'                 => __( 'Lesson', '1fix' ),
        'labels'                => $labels,
        'hierarchical'          => false,
        'public'                => true
    );
    register_post_type( 'lesson', $args );
}
add_action( 'init', 'my_register_cpt' );

And then you need to add meta box to relate the parent post type to the child post type:

<?php
function my_add_meta_boxes() {
    add_meta_box( 'lesson-parent', 'Course', 'lesson_attributes_meta_box', 'lesson', 'side', 'high' );
}
add_action( 'add_meta_boxes', 'my_add_meta_boxes' );

function lesson_attributes_meta_box( $post ) {
    $post_type_object = get_post_type_object( $post->post_type );
    $pages = wp_dropdown_pages( array( 'post_type' => 'course', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __( '(no parent)' ), 'sort_column'=> 'menu_order, post_title', 'echo' => 0 ) );
    if ( ! empty( $pages ) ) {
        echo $pages;
    }
}

After that you need to set the permalink structure:

<?php
function my_add_rewrite_rules() {
    add_rewrite_tag('%lesson%', '([^/]+)', 'lesson=');
    add_permastruct('lesson', '/lesson/%course%/%lesson%', false);
    add_rewrite_rule('^lesson/([^/]+)/([^/]+)/?','index.php?lesson=$matches[2]','top');
}
add_action( 'init', 'my_add_rewrite_rules' );

And finally, update the permalink of the post:

<?php
function my_permalinks($permalink, $post, $leavename) {
    $post_id = $post->ID;
    if($post->post_type != 'lesson' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
        return $permalink;

    $parent = $post->post_parent;
    $parent_post = get_post( $parent );

    $permalink = str_replace('%course%', $parent_post->post_name, $permalink);

    return $permalink;
}
add_filter('post_type_link', 'my_permalinks', 10, 3);
We are here to answer your question about WordPress one custom post permalink structure inside other custom post permalink - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji