Support

Account

Forum Replies Created

  • If your create_new_user_posts() function above works, try adding the below to your code; substitute your field keys and post type.

    function pre_save_company_name($post_id){
      if( empty($_POST['acf']) ) { return; }
      $company = $_POST['acf']['THE COMPANY NAME FIELD KEY'];
      return $company;
    }
    add_action('acf/save_post', 'pre_save_company_name', 21);
    function company_name_as_post_title( $data, $postarr ) {
        if ( ! $postarr['ID'] ) return $data;
        if ( $postarr['post_type'] !== 'YOUR POST TYPE' ) return $data;
        $post_id = $postarr['ID'];
        $company = pre_save_company_name($post_id);
        if( empty($company) ) { return $data; }  
        $data['post_title'] = $company;
        return $data;
    }
    add_filter( 'wp_insert_post_data', 'company_name_as_post_title', 99, 2 );
  • Here is how I would go about it, untested code.

    First a function to extract the names from $_POST, by adding an action to acf/save_post()

    function pre_save_user_fullname($post_id){
      if( empty($_POST['acf']) ) { return; }
      $firstname = $_POST['acf']['FIRST NAME FIELD KEY'];
      $lastname = $_POST['acf']['LAST NAME FIELD KEY'];
      $fullname = $firstname.' '.$lastname;
      return $fullname;
    }
    add_action('acf/save_post', 'pre_save_user_fullname', 21);

    Then modifying the post data before it is written to db, by filtering wp_insert_post_data();

    function user_fullname_as_post_title( $data, $postarr ) {
        if ( ! $postarr['ID'] ) return $data;
        if ( $postarr['post_type'] !== 'YOUR_POST_TYPE' ) return $data;
        $post_id = $postarr['ID'];
        $fullname = pre_save_user_fullname($post_id);
        if( empty($fullname) ) { return $data; }  
        $data['post_name'] = $fullname;
        return $data;
    }
    add_filter( 'wp_insert_post_data', 'user_fullname_as_post_title', 99, 2 );

    If it doesn’t work, I hope it at least helps.

  • Thanks John,

    Your solution solved the question as asked.
    Additionally, the problems you mentioned helped me realize that my plugin was poorly designed. The ACF Options Page turned out to be a much better, and much faster solution for what I am trying to accomplish ( no more get_posts() in my plugin file ).

    Cheers!

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