add_rewrite_rule and passing variables for custom post types

this is driving me crazy!!

Scenario is…

Read More

Custom post type let’s call it “cpt_product” setup with the rewrite “products”

Trying to use the add_rewrite_rule to create a friendly URL for meta details pertaining to the product. For example, this custom post type has 3 meta fields associated with each post (mt_details, mt_inventory, and mt_availability)

Desired URL sequence is

domain.com/products/product-name/details/

domain.com/products/product-name/inventory/

domain.com/products/product-name/availability/

This would allows access to a parameter on the front-end which would allow the highlight of that information.

This is the closest I’ve gotten just need a little help please!

    add_action('init','yoursite_init');
function yoursite_init() {
  global $wp,$wp_rewrite;
  $wp->add_query_var('metahighlight');
  $wp_rewrite->add_rule('product/([^/]+)/details',
'index.php?metahighlight=details&post_type=cpt_product&name=$matches[1]', 'top');

  // Once you get working, remove this next line
  $wp_rewrite->flush_rules(false);  
}

I did use this method although it didn’t seem to help me… just trying to get a single one of these nice url’s working.

When I access the URL:

www.sitename.com/index.php?metahighlight=details&post_type=cpt_product&name=product-name

WordPress returns

www.sitename.com/products/product-name/?metahighlight=details&post_type=cpt_product

When what I’m looking for is
www.sitename.com/products/product-name/details/

Any suggestions would be greatly appreciated.

Related posts