How to add custom taxonomy in custom post type permalink?
Asked Answered
O

2

8

I have a custom taxonomy called campaign and a custom post type called asset. For assets, I want to have the following permalink structure: mysite.com/<campaign_name>/<asset_name>. I have achieved this by the following code, but now if I go to any normal page with the url structure mysite.com/<pagename> it gives a 404. And when I comment out the rewrite slug part in the function for registering the custom post type, or add this instead ams/%campaign%, it works but that's not the URL structure I want for my custom post type.

Code for registering custom taxonomy:

...
'rewrite' => array(
    'slug' => '',
    'with_front' => true,
),
...

Code for registering custom post type:

...
rewrite' => array(
    'slug' => '%campaign%',
    'with_front' => true,
),
...

Functions for rewrite rules:

function ams_asset_add_rewrite_rules( $rules ) {
    global $post;
    if ($post->post_type == 'asset' ) {
        $new = array();
        $new['([^/]+)/(.+)/?$'] = 'index.php?asset=$matches[2]';
        $new['(.+)/?$'] = 'index.php?campaign=$matches[1]';
        return array_merge( $new, $rules );
    }
    return $rules;
}
add_filter( 'rewrite_rules_array', 'ams_asset_add_rewrite_rules' );

// Handle the '%campaign%' URL placeholder
function ams_asset_filter_post_type_link( $link, $post = 0 ) {
    if ( $post->post_type == 'asset' ) {
        $cats = wp_get_object_terms( $post->ID, 'campaign' );
        if ( $cats ) {
            $link = str_replace( '%campaign%', $cats[0]->slug, $link );
        }
    }
    return $link;
}
add_filter( 'post_type_link', 'ams_asset_filter_post_type_link', 10, 2 );
Ordonnance answered 3/9, 2019 at 5:22 Comment(0)
A
2

First, register your taxonomy and set the slug argument of rewrite to shows:

register_taxonomy(
    'campaign',
    'asset',
    array(
        'rewrite' => array( 'slug' => 'shows', 'with_front' => false ),
        // your other args...
    )
);

Next, register your post type and set the slug to shows/%show_category%, and set has_archive argument to shows:

register_post_type(
    'show',
    array(
        'rewrite' => array( 'slug' => 'campaign/%asset%', 'with_front' => false ),
        'has_archive' => 'asset',
        // your other args...
    )
);

Last, add a filter to post_type_link to substitute the show category in individual show permalinks:

function wpa_show_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'asset' ){
        $terms = wp_get_object_terms( $post->ID, 'campaign' );
        if( $terms ){
            return str_replace( '%campaign%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );
Alger answered 9/9, 2019 at 11:41 Comment(1)
custom post type archive page not workingBenthos
R
0

First, register your taxonomy, then register your custom post type. You may also need to save your permalinks. I don't think you want 'with_front' set to true. The rest may be ok, but here's a working example.

/**
 * Register a 'campaign' taxonomy for post type 'asset'.
 *
 * @see register_post_type for registering post types.
 */
function wpdocs_create_campaign_tax_rewrite() {
    register_taxonomy( 'campaign', 'asset', array(
        'rewrite' => array(
            'slug' => '',
            'with_front' => false,
        ),
    ) );
}
add_action( 'init', 'wpdocs_create_campaign_tax_rewrite', 0 );


/**
 * Implements init_hook to register post type.
 * https://codex.wordpress.org/Function_Reference/register_post_type
 */
function generic_create_posttype_asset() {
    register_post_type(
        'asset',
        array(
            'labels'       => array(
                'name'          => __( 'Assets' ),
                'singular_name' => __( 'Assets' ),
                'add_new'       => __( 'New Asset' ),
                'add_new_item'  => __( 'New Asset' ),
                'edit_item'     => __( 'Edit Asset' ),
            ),
            'description'  => 'Campaign ...',
            'public'       => true,
            'hierarchical' => true,
            'has_archive'  => true,
            'rewrite' => array(
                'slug' => '%campaign%',
                'with_front' => false,
            ),
            'show_in_menu' => true,
            'menu_icon'    => 'dashicons-category', // https://developer.wordpress.org/resource/dashicons/ .
            'supports'     => array( 'title', 'editor', 'revisions', 'thumbnail' ),
        )
    );
}
add_action( 'init', 'generic_create_posttype_asset' );

/**
 * Rewrite '%campaign%' with taxonomy assigned.
 */
function asset_show_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'asset' ){
        $terms = wp_get_object_terms( $post->ID, 'campaign' );
        if( $terms ){
            return str_replace( '%campaign%' , $terms[0]->slug , $post_link );
        }
    } else {
            return $post_link;
    }
}
add_filter( 'post_type_link', 'asset_show_permalinks', 1, 2 );

function generic_cpt_rewrite_flush() {
    generic_create_posttype_asset();
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'generic_cpt_rewrite_flush' );
Relive answered 6/9, 2019 at 21:57 Comment(2)
Hi thank you but I had tried this previously. This works for the custom post types but whenever I go to a page, it gives a 404.Ordonnance
odd, it worked for me. did you try going to settings > permalinks, then save permalinks?Relive

© 2022 - 2024 — McMap. All rights reserved.