dynamic slug
-
Hi, I need to change the slug for a custom post type, I have “products” with custom meta “brand”
products_post_type(){
$args = array(
‘hierarchical’ => false,
‘rewrite’ => array(‘slug’ => ‘%brand_slug%’, ‘with_front’ => false),
}
// add_rewrite_tag(‘%brand_slug%’,'([^&]+)’) . /*commented*/
register_post_type(‘products’, $args);
}
add_action( ‘init’, ‘products_post_type’, 0 );//to change the ‘%brand_slug%’ I use a link filter
function my_post_type_link_filter_function($post_link, $id = 0, $leavename = FALSE) {
if (strpos(‘%brand_slug%’, $post_link) === FALSE) {
$post = &get_post($id);
$brand = get_the_title(get_post_meta($post->ID,’brand’,true));
if(empty($brand)){$brand = ‘default’;}
$post_link = str_replace(‘%brand_slug%’,$brand, $post_link);
return $post_link;
}
}
add_filter(‘post_type_link’, ‘my_post_type_link_filter_function’, 1, 3);all this change the “products” post link in the admin, when I add a new product post the permalink show like this for example
Permalink: http://dev.wordpress/brandname/milk/
where brandname is the brand saved in the custom meta and milk is the name of the product.
when I access to that URL I get a 404 not found, (rewite rules already reset by re-saving the permalinks in admin panel)if I add “add_rewrite_tag(‘%brand_slug%’,'([^&]+)’);” before register post type “products” the url “http://dev.wordpress/brandname/milk/” work perfecly but all post type “pages” (w) become Inaccessible and need pages working too
The topic ‘dynamic slug’ is closed to new replies.