Support

Account

Home Forums Front-end Issues Get the permalink of the new post in acf_form

Solved

Get the permalink of the new post in acf_form

  • Hello, I have an acf_form for my ‘post’ post type and I want the form to display the permalink of the new post in the form. I tried this code to display the permalink but it returns the permalink of the page that the acf_form in it. this code working good in back-end but in front-end no.

    add_filter('acf/load_field/name=permalink', function($field) {
        global $post;
        $permalink = get_the_permalink();
        // variant 1 (use get_post_meta instead of get_field to avoid conflicts)
        if( get_post_meta($post->ID, 'permalink', true) == '' ) {
            $field['value'] = $permalink;
        }
        
        // variant 2
        $field['default_value'] = $permalink;
        return $field;          
    });
  • There is no permalink for the new post created with acf_form() until the post is created.

  • the permalink based on %post_id%, my code working code in back-end, but in front-end dosen’t work. is the acf_form cant display the permalink or it is impossible from the beginning?

  • When you create a new post in the admin WP auto assigns the next available ID to the post, creates an auto draft, etc. There is a placeholder added to the DB.

    On the front of the site ACF does not do this. Nothing is created for the post in the database until you hit submit. You could guess at the permalink based on the title being entered, but since there is no post ID yet, yes, anything that requires that post ID is impossible from the beginning

  • So if i make the permalink based on the post title, can a field in the form by ajax or anything else display the permalink when I put the title of the post? maybe a button can refresh the value of the field by javascript so the field can get the permalink after I add the titel or something like that, is that possible?

  • Thank you! that was very useful for me. but I have another question, how can I echo the value of the title in the acf form in another input field out of acf form?

  • you can use the ACF JS API to get and update field values.

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Get the permalink of the new post in acf_form’ is closed to new replies.