1

this is driving me crazy!!

Scenario is...

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.

  • Thanks for the quick response @G. M. I updated the questions above to include more detail – Simon Oct 3 '13 at 23:33
  • 2
    This is absolutely normal. You have to go to www.sitename.com/products/product-name/details/ and thanks to the rule it will be recognized just like you type www.sitename.com/index.php?metahighlight=details&post_type=cpt_product&name=product-name but showing in the browser the pretty url. But if you put in the browser the ugly url, WP will not replace it with the pretty one: if you want this you should implement the redirection by yourself. In short rewrite rules works showing pretty url and recognizing vars just like the ugly url was in the browser. – gmazzap Oct 3 '13 at 23:43

Your Answer

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

Browse other questions tagged or ask your own question.