WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

Currently I have two post types, "products" and "products-data" they are separate post types; however, for all intents and purposes products-data is a child post of products. The relation is established via a meta element for products-data "parent" that will have a products post's post ID.

The "products-data" posts are created only one way --- a user clicks on a product in the products post type and enters data into a popup window. This uses wp_insert_post to create a products-data post and it adds a meta element "parent" with the value of the "products" post's ID. This data is displayed via a modal window if a user clicks on the product type.

I am trying to have their permalinks as follows:

Permalink schema for a "products" post (for this example this post's ID is 123):


Permalink schema for a "products-data" post that has a meta key/value of parent=>123


Here is the code I am using to register the post type and create rewrite rules; however, after adding this code and flushing my rewrite rules (settings>permalink) it works fine for a "products-data" post type BUT when I try to access a "products" post it displays the homepage for the WordPress site (N.B. this is not via redirect, the URL bar displays the "products" URL).

I am wondering if the issue is that the two permastructs appear that they can collide with each other, however there will never be an instance of this in practice due to the permastructs schema.

Any help would be greatly appreciated on how to resolve this.


add_action ( 'init', 'register_product_post_types' );
add_action ( 'init', 'products_add_rewrite_rules' );
add_filter ( 'post_type_link', 'products_permalinks', 10, 3 );

function register_product_post_types() {    
    register_post_type( 'products',
                'supports' => array( 'title', 'editor', 'author',"custom-fields", "thumbnail", "excerpt", 'comments' ),
                'labels' => array(
                'name' => __( 'Products' ),
                'singular_name' => __( 'Products' )
        'public' => true,
        'has_archive' => false,
        'rewrite' => FALSE,

    register_post_type( 'products-data',
                'supports' => array( 'title', 'editor', 'author',"custom-fields" ),
                'labels' => array(
                'name' => __( 'Products-Data' ),
                'singular_name' => __( 'Products-Data' )
        'show_in_menu' =>'edit.php?post_type=products',
        'public' => true,
        'has_archive' => false,
        'rewrite' => FALSE,

function products_add_rewrite_rules() {
    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag('%products%', '([^/]+)', 'products=');
    $wp_rewrite->add_rewrite_tag('%products-data%', '([^/]+)', 'products-data=');
    $wp_rewrite->add_rewrite_tag('%parent%', '([^/]+)', 'parent=');
    $wp_rewrite->add_permastruct('products-data', '/products/%parent%/%products-data%', false);


function products_permalinks($permalink, $post, $leavename) {
    $no_data = 'no-data';
    $post_id = $post->ID;
    if($post->post_type != 'products-data' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
        return $permalink;
    $data = sanitize_title(get_the_title(get_post_meta($post_id, 'parent', true)));
    if(!$data) $data = $no_data;
    $permalink = str_replace('%parent%', $data, $permalink);
    return $permalink;
share|improve this question

See my answer here: http://wordpress.stackexchange.com/a/100486/12324.

The issue you're facing is that permastructs are adding multiple custom post types to the URL, which is confusing WordPress. You'll need to add your rewrite rules using add_rewrite_rule().

share|improve this answer

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.