Support

Account

Home Forums Front-end Issues Front End Form changes slug to post id rather then post title

Solved

Front End Form changes slug to post id rather then post title

  • I created a front end form and want to create the page title via a field. When i submit the form, in the dashboard the Page Name shows in the Post Title, however the permalink of the page shows the post id.

    example.

    http://www.mywebsite.com/cpt/174

    when it should show

    http://www.mywebsite.com/cpt/my-page-title
    —-

    My functions.php

    add_filter(‘acf/pre_save_post’, ‘save_post_from_frontend’);
    function save_post_from_frontend( $post_id) {

    //Check if user loggin or can publish post
    if( ! ( is_user_logged_in() || current_user_can(‘publish_posts’) ) ) {
    return;
    }

    // check if this is to be a new post
    if( $post_id!= ‘firm-profile’) {
    return $post_id;
    }

    $post= array(
    ‘post_type’ => ‘providers’, // Your post type ( post, page, custom post type )
    ‘post_status’ => ‘publish’, // (publish, draft, private, etc.)
    ‘post_title’ => $_POST[‘fields’][‘field_5e0b90db10390’]

    );
    // insert the post
    $post_id= wp_insert_post( $post);
    $_POST[‘return’] = add_query_arg( array(‘post_id’ => $post_id), $_POST[‘return’] );
    // Save the fields to the post
    do_action( ‘acf/save_post’, $post_id);
    return $post_id;

    }

    My Custom Template:

    <div class=” form-group”>
    <label for=”text” class=” control-label”><?php esc_html_e(‘Firm Profile’,’falcons’); ?></label>
    <div class=” “>
    <?php
    $new_post= array(
    ‘post_id’ => ‘firm-profile’, // Unique identifier for the form
    // PUT IN YOUR OWN FIELD GROUP ID(s)
    ‘field_groups’ => array(594), // Create post field group ID(s)
    ‘form’ => true,
    ‘submit_value’ => ‘Submit Post’,
    ‘updated_messag’ => ‘Saved!’
    );
    acf_form( $new_post);
    ?>

    <?php acf_form(); ?>

    </div>
    </div>

    —- Versions —-
    ACF v5.8.7
    WP v5.3.2

  • Where you once using an older version of ACF? 4 perhaps?

    This is incorrect

    
    'post_title' => $_POST['fields']['field_5e0b90db10390']
    

    $_POST['fields'] was used in ACF versions before 5. In ACF 5 or higher you need to use $_POST['acf']

    Your code is setting the post title to an empty value

    use this

    
    'post_title' => $_POST['acf']['field_5e0b90db10390']
    

    Also, do not do this

    
    do_action('acf/save_post', $post_id);
    

    This happens automatically for every post that is inserted, calling this yourself means that it will happen twice.

Viewing 2 posts - 1 through 2 (of 2 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.