Support

Account

Home Forums ACF PRO Dynamic pre_save_post possible?

Unread

Dynamic pre_save_post possible?

  • Hi,

    I’m near to finish my custom front-end form for post submission, but I encounter a problem regarding the pre_save_post filter.

    I’m working with a dozen of custom post types, and I really need to be able to change this $post = array(
    ‘post_type’ => ‘logiciels’,

    );

    of my pre_save_post filter.

    In order for you to better understand, I dynamicly register forms part with acf_register_form(), in order to have a total control of HTML wrappers for each fields group.

    I have then to call this filter in my functions.php file in order for the data to be saved :

    add_filter('acf/pre_save_post' , function ($post_id) {
        // Bail if not logged in or not able to post
        if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
          return;
       }
    
       // check if this is to be a new post
       if( $post_id != 'new_post' ) {
         return $post_id;
       }
    
        // Create a new post
        $post = array(
            'post_type' => 'logiciels',
            'post_status' => 'private',
            'post_title' => wp_strip_all_tags($_POST['acf']['field_5c5e9647cd28f']),
            'post_content' => $_POST['acf']['field_5c5e9678cd290'],
      );
    
        // insert the post
        $post_id = wp_insert_post( $post );
    
        // Save the fields to the post
        do_action( 'acf/save_post' , $post_id );
    
        return $post_id;
      });

    The problem is with $post, I need to have a dynamic value for the ‘post_type’ arg, instead of a fixed one ‘post_type’ => ‘logiciels’. And because this is a filter, it can only be executed in an included file like functions.php, not in the page.php itself which will have solved this problem (because I could use variable available at this scope).

    Any idea to help me?

Viewing 1 post (of 1 total)

The topic ‘Dynamic pre_save_post possible?’ is closed to new replies.