0

I need your help. I need to create a similar structure mentioned on this thread and below are the details of my project requirement:

I have three custom post types, let's say company-directory, video and articles. Each share a common taxonomy, let's call it tax.

The way I want to set up the URL is:

"tax_term" is the term name that belongs to the common taxonomy.

Company Directory stores info for company profiles. This will have the default URL, i.e. site.com/company-directory

For CPT "video" - I would want the URL to be site.com/company-directory/tax_term/video/post_slug

For CPT "articles" - site.com/company-directory/tax_term/articles/post_slug

I tried the exact solution @RachelCarden provided and it works with CPT "video" but CPT "article" is redirecting to homepage (301). Below is the code I'm working on:

add_action( 'init', 'my_website_add_rewrite_tag' );
function my_website_add_rewrite_tag() {
//Matching the url with the custom structure
//Custom URL for videos
add_rewrite_rule( '^company-directory/([^/]*)/([^/]*)/([^/]*)/?','index.php?company-video=$matches[3]','top' );
//Custom URL for reports
add_rewrite_rule( '^company-directory/([^/]*)/([^/]*)/?','index.php?report-presentation=$matches[2]','top' );
}

// this filter runs whenever WordPress requests a post permalink, i.e. 
get_permalink(), etc.
// we will return our custom permalink for 'videos' and 'reports'. 
//'company-directory' is already good to go since we defined its rewrite slug in the CPT definition.
add_filter( 'post_type_link', 'my_website_filter_post_type_link', 1, 4 );
function my_website_filter_post_type_link( $post_link, $post, $leavename, $sample ) {

switch( $post->post_type ) {

    case 'company-video':

        if ( $term = array_shift( wp_get_object_terms( $post->ID, 'trading-symbol' ) ) ) {
            if ( isset( $term->slug ) ) {
                // create the new permalink
                $post_link = home_url( user_trailingslashit( 'company-directory/' . $term->slug . '/videos/' . $post->post_name ) );
            }
        }

    break;

    case 'report-presentation':

    if ( $term2 = array_shift( wp_get_object_terms( $post->ID, 'trading-symbol' ) ) ) {

        if ( isset( $term2->slug ) ) {
            // create the new permalink
            $post_link = home_url( user_trailingslashit( 'company-directory/' .$term2->slug. '-report-presentation/' . $post->post_name ) );
        }

    }
        break;

}
return $post_link;

}

I was hoping someone could guide me on solving this. Thanks.

1 Answer 1

1

OK so after scratching my head for a day, I decided to rewrite the function. After searching for few minutes, I came across this tutorial that outlined the solution for me. It works like a charm. I hope it helps you guys too.

Your Answer

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

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