WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 1 down vote accepted

I changed this line:

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


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

Wich was for both issue 1 and 2 enough to work with.

share|improve this answer
thanks mate - helpfull that you to supplied your solution – Sagive SEO Apr 24 '13 at 11:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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