Support

Account

Home Forums General Issues Publish new post & redirect issue

Solved

Publish new post & redirect issue

  • Hello,
    I’m trying to create a simple solution that will allow the user to publish a new post from frontend and will redirect them to the published post after all. I searched through the forums (and google)and I found some solutions, but none of them is working in my case.

    All I get is that user is redirected to the published post, but it is empty (custom field values are not saved ?)

    This is what I have in functions.php:

    
    function my_pre_save_post( $post_id )
    {
        // check if this is to be a new post
        if( $post_id != 'new' )
        {
            return $post_id;
        }
     
        // Create a new post
        $post = array(
            'post_status'  => 'publish' ,
            'post_title'  => 'title' ,
            'post_type'  => 'post' ,
        );  
     
     $post_id = wp_insert_post( $post ); 
     
     do_action('acf_save_post', $post_id);
    
    $permalink = get_permalink($post_id);
    wp_redirect($permalink);
    exit;
    
       
        return $post_id;
    }
     
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    

    and on front-end page:

    
    <?php acf_form(array(
                    'post_id'  => 'new' , 
                    'field_groups'  => array(177), 
                )); ?>
    

    I’m positive that something is missing or that I did something wrong but I’ll be really grateful if somebody could help me. I’m not a php expert as well 🙂

  • Hi @martines

    You are using the incorrect action:
    do_action('acf_save_post', $post_id);

    should be:

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

    Please check the core/api.php source code for more.

    Thanks
    E

  • Exactly what i was looking for all day 🙂

    This really should up highlighted somehow.

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

The topic ‘Publish new post & redirect issue’ is closed to new replies.