I have a drop down select type field. The values are Video and Image.
I want to replace the Custom post type slug. Currently, it is video.
I want to change mysite.com/video/post-name to mysite.com/1/post-name or mysite.com/0/post-name
I mean when the drop down value Video is selected, it will show mysite.com/1/post-name.
I can replace the post type slug.
function update_video_slug( $args, $post_type) {
$cptslug = “newslug”;
if ( ‘video’ === $post_type ) {
$args[‘rewrite’][‘slug’] = $cptslug;
}
return $args;
}
add_filter( ‘register_post_type_args’, ‘update_video_slug’, 10, 2 );
Can be the $cptslug value dynamic according to ACF value?
It would depend on the order that things happen. Usually you add custom post types on the init hook. ACF also initializes on the init hook. To use get_field() ACF needs to be initialized before you try to get the value. You may also run into an issue if you try to get a field before ACF fires the acf/include_fields hook.
In this case, assuming that this is on an options page, I would use the built in WP function get_option() to get the ACF field value. Usually on an options page the option name will be “options_YOUR-FIELD-NAME”. Doing this you don’t need to worry about ACF when you register your post type.
I want to get values directly from the ACF fields.
How to initialize the ACF in init hook? Any example?
Basically, you need to make sure that you’re creating your post types after acf has initialized. You can do this by adding your action that creates the post types with a sufficiently high priority.
add_action('init', 'my_register_post_types', 20); // priority 20 is after acf/init
function my_register_post_types() {
// register your post types
// get_field() should work
}