Home Forums ACF PRO How to replace custom post type slug by an ACF value?


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

    • shohag

    • December 3, 2018 at 1:43 am

    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 to or

    I mean when the drop down value Video is selected, it will show

    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.

    • shohag

    • December 3, 2018 at 2:35 am

    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
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.