最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to replace custom post type slug with an ACF value?

matteradmin9PV0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 4

Can be the $cptslug value dynamic according to ACF value?

Yes, it can.

Here's how:

  1. First off, my ACF field settings are as follows:

    Name: video_or_image
    Type: Select
    Options:

    1 : Video
    0 : Image
    
  2. 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%.

  3. Next, I register the rewrite tag (%video_or_image%) after the register_post_type() call, which is hooked to the init 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:

  4. 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

  1. 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 );
    
  2. 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+).

Post a comment

comment list (0)

  1. No comments so far