I'm using wp-types to build a wordpress site. I'm happy with the types and their fields, I just wanted to created a function that shows parents in the url - see blow function that achieves this - also using the Custom Post Type Permalinks plugin to add my custom tag %parents% to the url.

The rewrite seems to work fine, displays in the back end etc. But when you click view document - 404.php.

I've tried rewrite flushing - mod rewrite definitely works, as friendly permalinks work for pages and don't 404, it's only on the custom post types.

My ideal url structure is: %sector%/%parents%/%postname% - Sector being a custom taxonomy. The custom taxonomy also displays fine because of the CPTP plugin but 404s as well.

wp-types does have rewrite functions but they are all turned off. it's really got me stumped, the code below is included in my functions.php - I am using bones starter theme.

Should I include this code as a plugin instead? Or is there something i've missed? The only way I can get access the items is by setting permalinks to default.

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

  function parents_post_rewrite_function( $post_link, $id = 0, $leavename = FALSE ) {
    $post = get_post($id);
    $postID = $post->ID;

    // Always globalize $wpdb
    global $wpdb;

    // Get current posts parent type
    $parentType = $wpdb->get_var("SELECT  `meta_key` FROM  `wp_postmeta` WHERE  `post_id` ={$postID} AND `meta_key` LIKE '%belongs%'");

    // Get current posts parentID if project
    $parentID = $wpdb->get_var("SELECT  `meta_value` FROM  `wp_postmeta` WHERE  `post_id` ={$postID} AND  `meta_key` =  '{$parentType}'" );


    // Get parent parent posts type
    $parentParentType = $wpdb->get_var("SELECT  `meta_key` FROM  `wp_postmeta` WHERE  `post_id` ={$parentID} AND `meta_key` LIKE '%belongs%'");

    // Try and get same value from parent ID, if fails then no parent of parent 
    $parentParentID = $wpdb->get_var("SELECT  `meta_value` FROM  `wp_postmeta` WHERE  `post_id` ={$parentID} AND  `meta_key` =  '{$parentParentType}'" );


            $parentslug = get_post($parentID)->post_name;
            $parentparentslug = get_post($parentParentID)->post_name;
            $parents = $parentparentslug.'/'.$parentslug;
    } else {
        $parents = get_post($parentID)->post_name;

    // Get parents parent post ID & continue until none found
    // Convert IDs to names and render slug

    return str_replace('%parents%', $parents, $post_link);
  • 1
    the post_type_link filter is just for functions that output permalinks, it has nothing to do with generating rewrite rules for incoming requests for those permalinks.
    – Milo
    Commented Feb 6, 2015 at 6:55
  • I see. I'm new to wordpress, what should I be using to get it working? Commented Feb 8, 2015 at 10:52

1 Answer 1


I had this same problem when I cloned my site to set up a development server - for me, it working by setting permalinks to ugly, saving, then setting them to pretty again, which rebuilt whatever had gone wrong in htaccess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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