Title: dynamic slug
Last modified: December 7, 2016

---

# dynamic slug

 *  [seelescript](https://wordpress.org/support/users/seelescript/)
 * (@seelescript)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/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/](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/&#8221](http://dev.wordpress/brandname/milk/&#8221);
   work perfecly but all post type “pages” (w) become Inaccessible and need pages
   working too

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/dynamic-slug/#post-8531048)
 * You need to have a permalink scheme where a regexp can readily distinguish a 
   product permalink from a page permalink. Position based schemes only work when
   the positioning is identical throughout the site. As in the slug is always in
   the n_th_ position in all cases. Not easy to accomplish with most sites.
 * What generally works better is having some fixed element that would distinguish
   products from pages. example.com/**page**/about-us/ or example.com/**product**/
   brandname/milk/ where the bold element is the same for all posts of that type.
   It only needs to be a single letter if that helps any.
 * Then use add_rewrite_rule() where the regexp picks up on this fixed element and
   rewrites the request with an explicit post_type argument. Don’t forget to save
   permalinks after changing anything rewrite related 🙂
 *  Thread Starter [seelescript](https://wordpress.org/support/users/seelescript/)
 * (@seelescript)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/dynamic-slug/#post-8532674)
 * what about if I use dev.wordpress/hardtext-brandname/milk/ where brandname is
   brand in custom field
 * can you help me with the rule for that please
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 6 months ago](https://wordpress.org/support/topic/dynamic-slug/#post-8532878)
 * I think something like this would do it:
 *     ```
       function custom_rewrite_rule() {
          add_rewrite_rule('^hardtext-([^/]*)/([^/]*)/?', 'index.php?meta_key=brand&meta_value=$matches[1]&name=$matches[2]&post_type=products', 'top');
       }
       add_action('init', 'custom_rewrite_rule');
       ```
   
 * With this, if you request _example.com/hardtext-brandname/milk/_ , it will be
   rewritten to _example.com/index.php?meta\_key=brand&meta\_value=brandname&name
   =milk&post\_type=products_ . If that doesn’t match the query vars for your site,
   adjust accordingly so that the rewritten URL returns the desired products post.
   I imagine you would use something besides “hardtext-” as the fixed element. You
   can see how to alter this element to whatever you like in the regexp (first) 
   parameter for [add_rewrite_rule()](https://codex.wordpress.org/Rewrite_API/add_rewrite_rule).
 * If you have trouble getting this to work, please post an index.php-plus-parameters
   type of URL that actually returns the desired post.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘dynamic slug’ is closed to new replies.

## Tags

 * [custom post](https://wordpress.org/support/topic-tag/custom-post/)
 * [rewrite slug](https://wordpress.org/support/topic-tag/rewrite-slug/)
 * [slug](https://wordpress.org/support/topic-tag/slug/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [9 years, 6 months ago](https://wordpress.org/support/topic/dynamic-slug/#post-8532878)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
