I would like to create custom post URL from Custom Post Types variable. I considered a way to do that:

In Permalinks settings, I see there are some default tags you can choose to compound URL from (%year%, %post_id%, %postname% etc.). I was thinking about creating custom tag you can include(e. g. %mytag%) or rewriting existing one. That tag should be automatically generated based on variables from CPT.

So the final tag should be:

 %mytag% = $variable1 .  '_' . $variable2

Is this possible to do? Maybe there is much easier way to do that, so please let me know how would you solve this issue.

Thank you very much for your answers!

  • What are you expecting this to look like when it works? is it just a front before post URLs for your CPT or are you expecting this to change on each individual post?
    – mrben522
    Jan 16, 2019 at 16:52
  • I expect it to change on every post. Lets say I have Custom Post Type "Cars" with Advanced Custom Fields "Brand" ($variable1) and "License Plate" ($variable2). I fill Brand="Ford" and License Plate="ABCDE". The URL of the post should look like website/cars/ford-ABCDE. Hope this is clear.
    – PandaBoii
    Jan 16, 2019 at 17:32

1 Answer 1


Instead of creating a new tag, I would suggest just writing code to create the slug on a per-post basis using ACF's save_post function.

//Update Cars slug field:
function wpse_post_slug_updater( $post_id ) {

    if( get_post_type() == 'cars') {
        $my_post = array();
        $my_post['ID'] = $post_id;
        $my_post['post_title'] = esc_html(get_the_title($post_id));

        $my_new_slug = get_field('brand') . ' ' . get_field('license_plate');
            $my_post['post_name'] = sanitize_title($my_new_slug);
            wp_update_post( $my_post );


// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'wpse_post_slug_updater', 20);
  • So what this function does is that rewrites/updates the original post slug (last URL part) right? I am quite noobie in WP, so I would like to ask for a bit more explanation of the code. I dont understand the line with sanitize_title(). I tried to google it, but that didnt help much. What exactly this sanitize function does? Many thanks.
    – PandaBoii
    Jan 16, 2019 at 19:31
  • Yes, this function is intended to rewrite the URL slug from whatever the title of your custom post type was to your custom fields. For instance, if your post was called "Cool New Car", but you wanted the slug to automatically become "Ford-KJ3P91", you could replace the get_field code ('brand' and 'license_plate') to the names of your ACF fields. The code will only run on a post being saved, and only for posts in the custom post type of 'cars'. Sanitize_title is a WordPress function which removes accent characters and other HTML, also replacing any spaces with dashes.
    – Chris B
    Jan 16, 2019 at 19:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.