• Problem in hand :

    I have custom categories in wordpress named “Product Categories” with custom post type “Products”.

    i want to change urls from

    somesite.com/product/airconditioner/voltas/productname
    to

    somesite.com/airconditioner/voltas/productname
    Code to register post and categories

    class aw_products_post_type {
    
    function __construct(){
       $this->aw_register_post_type();
       $this->aw_add_post_type_actions();
       $this->aw_add_post_type_filters();
    }
    
    public function aw_register_post_type(){
        // Labels
    $labels = array(
     'name' => __('Products','framework'),
    'singular_name' => __('Product','framework'),
    'add_new' => __('Add new','framework'),
    'add_new_item' => __('Add new product','framework'),
    'edit_item' => __('Edit','framework'),
    'new_item' => __('New product','framework'),
    'view_item' => __('View product','framework'),
    'search_items' => __('Search product','framework'),
    'not_found' =>  __('No product found','framework'),
    'not_found_in_trash' => __('No product found in trash','framework'),
    'parent_item_colon' => '',
    'menu_name' => __('Products','framework')
    );
    
    $short_url = (get_option('tz_products_short_url') != '') ?         get_option('tz_products_short_url') : 0;
    $slug_first_part = ((get_option('tz_custom_products_slug') != '') ?     get_option('tz_custom_products_slug') : 'product');
    if($short_url == 1) {
    $slug = $slug_first_part;
    } else {
    $slug = $slug_first_part."/%product_category%/%product_brand%";
    }
    
    // Arguments
    $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'menu_icon' => get_stylesheet_directory_uri().'/img/admin/admin-    products.png',
    'rewrite' => true,
    'capability_type' => 'post',
    'rewrite' => array("slug" => $slug), // Permalinks format
    'has_archive' => true,
    'hierarchical' => false,
    'menu_position' => null,
    'taxonomies' => array('post_tag'),
    'supports' => array('title','editor','author','thumbnail','excerpt', 'comments', 'tags')
    );

    Value of tz_custom_products_slug in wp_option -> Product

    Solutions tried in .htaccess

    <IfModule mod_rewrite.c>
    RewriteEngine on
    
    RewriteBase /
    RewriteRule ^product/(.+)$ /$1 [L,R]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
    </IfModule>

    The above solution didn’t worked.

    Solution 2 Tried and failed as follows:

    changed this from above code $slug = $slug_first_part.”/%product_category%/%product_brand%”;

    to this

    $slug = “/%product_category%/%product_brand%”;
    This caused 2nd custom taxanomy as page not found that is brands that shows urls like this :

    sitename.com/brands/acer/
    If you need the whole custom taxanomy code for brand and product_category let me know.

    To check the original urls check it on : https://www.pricereview.in/product/digital-cameras/olympus/olympus-sz-17-digital-camera/

    Similarly my custom taxanomy contains basename categories. i also want to remove it.

    This is how urls are working. i want to convert this

    https://www.pricereview.in/categories/mobile-price-in-india/

    to

    https://www.pricereview.in/mobile-price-in-india/

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You can’t have it both ways with rewrite rules. If you make a rewrite rule that says the first permalink term is for product categories, then all other requests not involving product categories will not work. If you have all other permalinks working, then you need to have something in the permalink that tells WP the next term is for product categories.

    While rewrite rules will not work for what you want, the ‘parse_request’ action has potential to work for you. I’ve not used this action for such a purpose, so I couldn’t say for sure that it will work, but it’s worth looking into. It fires after WP tries to parse the request, passing the WP object by reference. You can check if WP is going to return 404 (because it couldn’t find a post to match the first permalink term) because '404' == $wp->query_vars['error']

    Your callback could then try to match the first permalink term as a product category. If there’s a match, set the appropriate query var and unset the 404 error, otherwise return the error because no match was found by either WP or your callback.

Viewing 1 replies (of 1 total)
  • The topic ‘Rewrite Isuues in custom taxanomy’ is closed to new replies.