Question

I have the following issue:

Two Custom Post Types, lets call them cpt1 and cpt2.

In cpt2, I have a custom meta box in which you can select a post from cpt1, its post ID is stored in a meta value called postid.

The permalink structure of cpt2 is /%post_type%/%post_name%/.

What i'm trying to accomplish is:

  1. When postid has a post ID of cpt1 (wich will always be the case since it is a required field), the permalink structure of that specific post will become /selected_post_slug/%post_type%/%post_name%/.
  2. The cpt2 archive page must be available by /selected_post_slug/cpt2/, so it looks like every cpt1 page has its own cpt2 archive.

I managed to get point 1 to work by adding the following code in my functions.php file:

$wp_rewrite->add_rewrite_tag( '%selected_meta%', '([^&]+)', '?meta=' );

function extra_post_link( $permalink, $post, $leavename ) {
    if ( stripos( $permalink, '%selected_meta%' ) == false )
        return $permalink;

    if ( is_object( $post ) && $post->post_type == 'ervaring' ) {
        $_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 );

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

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

    return $permalink;
}

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

I followed a quick tutorial about adding rewrite tags in the permalink structure wich showed me how to figure out the first thing.

But, unfortunately, I'm still working on a solution for point 2.

Could I get some thought about this issue to help me a bit further down the road? Help would be much appreciated.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top