I have a drop down select type field in Advanced Custom Fields. The values are Video and Image.
I want to replace the Custom post type slug. Currently, it is video.
I want to change mysite/video/post-name to mysite/1/post-name or mysite/0/post-name
I mean when the drop down value Video is selected, it will show mysite/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?
I have a drop down select type field in Advanced Custom Fields. The values are Video and Image.
I want to replace the Custom post type slug. Currently, it is video.
I want to change mysite/video/post-name to mysite/1/post-name or mysite/0/post-name
I mean when the drop down value Video is selected, it will show mysite/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?
Share Improve this question asked Dec 2, 2018 at 14:48 NoobieNoobie 1091 gold badge2 silver badges10 bronze badges1 Answer
Reset to default 4Can be the
$cptslug
value dynamic according to ACF value?
Yes, it can.
Here's how:
First off, my ACF field settings are as follows:
Name:
video_or_image
Type: Select
Options:1 : Video 0 : Image
So I register the post type like so, where the rewrite slug is set to
%video_or_image%
— i.e.%{Name of the ACF field}%
, but you don't have to follow that format:register_post_type( 'video', array( 'label' => 'Video', 'public' => true, 'rewrite' => array( 'slug' => '%video_or_image%', ), ... ) );
Or if you want to or can only use the
register_post_type_args
to change the rewrite slug, then just set the$cptslug
to%video_or_image%
.Next, I register the rewrite tag (
%video_or_image%
) after theregister_post_type()
call, which is hooked to theinit
action:add_rewrite_tag( '%video_or_image%', '([^/]+)' );
Registering the tag is mandatory, and ensures that the tag is properly replaced with the RegEx pattern (i.e.
([^/]+)
) when the rewrite rules are saved:Then I replace the rewrite tag with the proper (ACF) field value using the
post_type_link
filter:add_filter( 'post_type_link', function( $url, $post ){ if ( 'video' === $post->post_type ) { $value = get_field( 'video_or_image', $post->ID ); $url = str_replace( '%video_or_image%', $value, $url ); } return $url; }, 10, 2 );
That's all, but don't forget to flush the rewrite rules — simply visit the permalink settings page.
And I suggest you to set a default value for your ACF field, and use "better" values if possible.. I mean, other than . =)1
or 0
.. but if you're sure about such values, then (I think) it's alright
UPDATE
In my case, using only ([^/]+)
in the rewrite tag, causes troubles when loading Pages (i.e. page
post type):
add_rewrite_tag( '%video_or_image%', '([^/]+)' );
How I solved the problem
Use a unique identifier, e.g.
v/
as in:add_rewrite_tag( '%video_or_image%', 'v/([^/]+)' );
Then in the post permalink,
v/
should also be added to the rewrite tag replacement value — see step #4 above:$url = str_replace( '%video_or_image%', 'v/' . $value, $url );
I restricted the ACF field value to numeric, then I used:
add_rewrite_tag( '%video_or_image%', '(\d+)' );
That
(\d+)
does not guarantee no conflict with other rewrite rules, but at least in my case, it worked.
Sorry about the ([^/]+)
— I thought the 0
and 1
values were just examples. I should've just used (\d+)
.