• 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

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    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 nth 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

    (@seelescript)

    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

    (@bcworkz)

    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().

    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.