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!
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!
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');
if(!empty($my_new_slug)){
$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);