Url Rewrite For custom post type with custom taxonomy

  • Unknown's avatar

    Hi All,

    I am getting Issue with URl rewrite with CPT,

    I need Below structured for CPT

    http://www.domainname.com/mobile-phones
    http://www.domainname.com/mobile-phones/apple
    http://www.domainname.com/mobile-phones/apple/apple-iphone-7
    http://www.domainname.com/mobile-phones/apple/apple-iphone-7/price-16gb-64gb
    http://www.domainname.com/mobile-phones/apple/apple-iphone-7/summary

    I have used my CPT name’devices’ with two taxonomy product type and brand
    register_post_type( ‘devices’,
    ‘public’ => true,
    ‘has_archive’ =>true,
    ‘publicly_queryable’ => true,
    ‘query_var’ => true,
    ‘rewrite’ =>false,
    ….

    );

    global $wp_rewrite;
    $projet_structure = ‘/%product-type%/%productbrand%/%devices%’;
    $wp_rewrite->add_rewrite_tag(“%devices%”, ‘([^/]+)’, “devices=”);
    $wp_rewrite->add_permastruct(‘devices’, $projet_structure, false);

    //Rewrite post type

    function rewrite_post_link($permalink, $post, $leavename){
    //If is custom post type “devices”
    if ($post->post_type == ‘devices’) {
    // Get current post object
    global $post;
    // Get current value from custom taxonomy “product-type”
    $producttypes = get_the_terms($post->id, ‘product-type’);
    $producttype = $producttypes[0]->slug;

    // Get current value from custom taxonomy “brands”
    $brands = get_the_terms($post->id, ‘brands’);
    $brand = $brands[0]->slug;
    // Re-structure permalink with string replace to include taxonomy value and post name
    $permalink = str_replace(‘%product-type%’.’/’, $producttype .’/’, $permalink);
    $permalink = str_replace(‘%productbrand%’.’/’, $brand .’/’, $permalink);
    }
    return $permalink;

    }
    add_filter( ‘post_type_link’, ‘rewrite_post_link’, 1, 3);
    add_filter(‘post_link’, ‘rewrite_post_link’, 1, 3);

    This method is working fine for ‘devices’ custom post type. But I am getting all post type redirected to 404 Page. Like post,page etc.
    Please help me for above structure

  • Unknown's avatar

    You’ve posted your question in the forums for sites hosted on WordPressdotcom, but it sounds to me like you are working with a site using the standalone WP software.

    Standalone WP and WordPressdotcom are different entities; more information about the differences can be found here: https://en.support.wordpress.com/com-vs-org/

    Help for self-hosted sites using the standalone WP software can be found over at https://codex.wordpress.org/Main_Page and at https://wordpress.org/support/ If you don’t have a username on the ORG forums, you can register one on that page.

  • The topic ‘Url Rewrite For custom post type with custom taxonomy’ is closed to new replies.