admin管理员组

文章数量:1122832

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


where product is a custom post type.

I want to change it to:


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.

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.

Share Improve this question edited Jan 20, 2017 at 7:02 Dave Romsey 17.9k11 gold badges55 silver badges70 bronze badges asked Jan 20, 2017 at 6:18 user2504941user2504941 111 silver badge6 bronze badges 2
  • Have you tried wordpress.org/plugins/custom-permalinks – Ravi Shinde Commented 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. – user2504941 Commented Jan 20, 2017 at 7:24
Add a comment  | 

1 Answer 1

Reset to default 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 );

本文标签: plugin developmentRemove custom post type slug from URL and add taxonomy Slug