I am lost with rewrite rules and need help please.
I created a custom post type "products" with a custom meta "brand".
The URL for a product called milk will be mysite.com/products/milk
, I need the URL include the brand like this, mysite.com/brand-brandname/milk
Some examples:
mysite.com/brand-supermilk/milk
where "supermilk" is the brand in the custom meta of milk
mysite.com/brand-goodproducts/coffe
where good products is the brand
I have this rule and rewrite:
$args = array(
'rewrite' => array('slug' => 'brand-%brand_name%', 'with_front' => false),
);
register_post_type('products', $args);
function wptuts_custom_tags() {
add_rewrite_rule("^brand-([^/]+)?",'index.php?post_type=products&brand_name=$matches[1]','top');
flush_rewrite_rules();
}
add_action('init','wptuts_custom_tags');
function my_post_type_link_filter_function($post_link, $id = 0, $leavename = FALSE) {
if (strpos('%brand_name%', $post_link) === FALSE) {
$post = &get_post($id);
$brand_name =get_post_meta($post->ID,'brand',true);
if(empty($brand_name)){$brand_name = 'default';}
$post_link = str_replace('%brand_name%',$brand_name, $post_link);
return $post_link;
}
}
add_filter('post_type_link', 'my_post_type_link_filter_function', 1, 3);
All this shows me correct URL in the admin when I add a new product post, but show me 404 or products list of post, but no the product page.