0

I am attempting to set up a relationship in Wordpress between two custom post types I’ve created and display that relationship within each post type’s permalink URL structure.

I have created two custom post types of solution_cpt and solution_offering_cpt. There is a one to many relationship from ’solution_cpt’ to ’solution_offering_cpt’. In other words, every ’solution_offering_cpt’ will never have more than one parent ’solution_cpt’. This is a one–to–one relationship.

The way I am relating each ‘solution_offering_cpt’ post to it’s pseudo ‘solution_cpt’ parent post is with a custom taxonomy. Every time a ‘solution_cpt’ post is created, I have a function that will create a new term and add it to my custom taxonomy. Of course, if the ‘solution_cpt’ post is deleted at a later date, then I also have a function to remove that term from the taxonomy.

The URL structure I am attempting to create is:

/solution/solution_cpt/solution_offering_cpt

The first level of /solution/solution_cpt is working and points to the correct post on the site. (The page it uses in my theme is 'single-solution_cpt.php'.) It's the URL to the second post type (ie. /solution/solution_cpt/solution_offering_cpt) that isn't working correctly and giving me a 404 error. (PS. I've flushed my permalinks several times.)

Here is how I’ve registered my two custom post types and set up my custom taxonomy:

// Register post types
function ta_create_post_types() {
    register_post_type(‘solution_cpt’, $labels,
        ‘hierarchical’ => false,
        'public' => true,
        'has_archive' => false,
        'rewrite' => array('slug' => 'solution'),
        'capability_type' => 'post'
    );
    register_post_type('solution_offering_cpt', $labels,
        'hierarchical' => false,
        'public' => true,
        'has_archive' => false,
        'rewrite' => array('slug' => 'solution/%solution_cat%'),
        'capability_type' => 'post'
    );
}
add_action('init', 'ta_create_post_types', 0);

// Create custom taxonomy
function ta_create_custom_taxonomies() {
    register_taxonomy('solution_cats', array('solution_cpt', 'solution_offering_cpt'), $labels,
        'hierarchical' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'solution')
    );
}
add_action('init', 'ta_create_custom_taxonomies', 0);

I also have two functions that control adding & removing terms from the 'solution_cats' custom taxonomy when a new 'solution_cpt' post is created or removed respectfully. I can post that code if it would benefit someone. ...just wanted to keep this initial inquiry as short as possible.

Note: that the terms that are created have mirrored titles and slugs of the created 'solution_cpt'. So, for example, if I were to create a 'solution_cpt' post of title "Creative Services", then a term would automatically be created and added to the taxonomy with a title of "Creative Services" and its slug set to "creative-services".

I'm assuming I need to use the Wordpress Rewrite API to pull of the URL structure I'm needing. Again, that structure is:

/solution/solution_cpt/solution_offering_cpt

Here's the current rewrite code I have:

// Build the 'solution_offering_cpt' URL
function ta_add_rewrite_rules() {
    global $wp_rewrite;

    $wp_rewrite->add_rewrite_tag('%solution_cat%', '([^/]+)', 'solution_cat=');
    $wp_rewrite->add_permastruct('offering', 'solution/%solution_cat%', false);
}
add_action('init', 'ta_add_rewrite_rules');

function filter_solution_offering_cpt_link($link, $post) {
    if ($post->post_type != 'solution_offering_cpt')
        return $link;

    if ($cats = get_the_terms($post->ID, 'solution_cats'))
        $link = str_replace('%solution_cat%', array_pop($cats)->slug, $link);
        return $link;
}
add_filter('post_type_link', 'filter_solution_offering_cpt_link', 10, 2);

All I get is a 404 error when trying to navigate to a 'solution_offering_cpt' post. An example is I have created a 'solution_cpt' post of title "Creative Services" and slug "creative-services", and a 'solution_offering_cpt' post of title "UI/UX" and slug "ui-ux".

When I navigate to /solution/creative-services/ui-ux I get a 404 error. I've flushed my permalinks. Not sure where I'm going wrong, but I'm sure my rewrites are off. The Wordpress Rewrite API is a bear and the documentation is terrible!! Would greatly appreciate help on this one guys & gals. Thanks!

  • your function to add rewrite tag and rules is redundant, registering your post types and taxonomy with rewrite argument generates all the rewrite tags and rules you need. get rid of ta_add_rewrite_rules and use the rewrite tag solution_cats (with an s) which was automatically generated when you registered your taxonomy. – Milo Mar 4 '14 at 16:45
  • Thanks for the response! I commented out the ta_add_rewrite_rules function and have my 'solution_offering_cpt' rewrite parameter set as array('slug' => 'solution/%solution_cats%') but am still getting 404s when trying to navigate to the 'solution_offering_cpt' posts. Also flushed my permalinks a few times to see if that was the issue. I still need my filter_solution_offering_cpt_link function, correct? In that function block, I changed line #6 to read $link = str_replace('%solution_cats%', array_pop($cats)->slug, $link); – kaffolder Mar 4 '14 at 17:02
  • here's a working example in this answer which is essentially the same as what you're doing here. I see a typo in your code above (missing quote after %solution_cat%), not sure if that's just an issue here or if your live code is broken as well. – Milo Mar 4 '14 at 18:16
  • Cool. Looks promising. I'll try getting this guy's setup working in the next hour or so. I also found this answer which looks promising as well. The missing apostrophe you caught was just in this post. I missed it when typing in my question. It's there in my existing code, so that's not the issue. Thanks for the heads up though. I edited the question so it's accurate now for anyone else that is reading and wants to weigh in. Thanks @Milo! Hopefully I'll get somewhere with one of those two posts. – kaffolder Mar 4 '14 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.