1

I am having an issue while changing a custom post type URL. Current post type URL is:

http://example.com/product/product-slug

where product is a custom post type.

I want to change it to:

http://domain.com/brand/brand-slug/product-slug

where brand is the custom taxonomy.

I found code that removes /product/ from the custom post type URL and it's working fine for me. I am using following code.

function gp_remove_cpt_slug( $post_link, $post, $leavename ) {

    if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }

    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

    return $post_link;
}
add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );


function gp_parse_request_trick( $query ) {

    // Only noop the main query
    if ( ! $query->is_main_query() )
        return;

    // Only noop our very specific rewrite rule match
    if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    // 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'page', 'product' ) );
    }
}
add_action( 'pre_get_posts', 'gp_parse_request_trick' );

Permalink setting is /%postname%/. But when I am changing the permalink setting to /brand/%brand%/%postname%/, all the custom post types start giving me 404 error.

Please suggest what changes need to be done in the above code to make it work.

2
  • Have you tried wordpress.org/plugins/custom-permalinks Jan 20, 2017 at 7:16
  • I used this plugin, but I have more then 5000 product that's automatically adding and removing. So I don't have time for manually change each product. Later on I will have more then 15000 product, that will be automatically add, so I don't want any manual work here. Jan 20, 2017 at 7:24

1 Answer 1

0

You can set up simple tax/cpt permalinks with the rewrite argument when you register the post type:

function wpd_product_brand_types() {

    register_taxonomy(
        'brand',
        'product',
        array(
            'rewrite' => array( 'slug' => 'brand', 'with_front' => false )
        )
    );

    register_post_type(
        'product',
        array(
            'label' => 'Products',
            'public' => true,
            'supports' => array( 'title' ),
            'rewrite' => array( 'slug' => 'brand/%brand%', 'with_front' => false )
        )
    );

}
add_action( 'init','wpd_product_brand_types' );

Then the post_type_link filter swaps in the %brand% slug:

function wpd_product_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'product' ){
        $terms = wp_get_object_terms( $post->ID, 'brand' );
        if( $terms ){
            return str_replace( '%brand%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpd_product_permalinks', 1, 2 );
4
  • I applied this solution, but all the custom type post redirecting to 404 error page. Feb 7, 2017 at 7:16
  • @user2504941 did you flush permalinks after changing them?
    – Milo
    Feb 7, 2017 at 16:20
  • Yes, I already tried that. Feb 8, 2017 at 17:40
  • update your question with the complete code you are currently using.
    – Milo
    Feb 9, 2017 at 3:16

Your Answer

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

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