Support

Account

Home Forums Front-end Issues Using acf field to define a new post content & title

Solving

Using acf field to define a new post content & title

  • Hi
    I create these 4 forms (forms karbar, mbm, tours, hotel) for user send form in frontend that works correctly.
    I use the below code for change the title and content but just the last part of code work.

    
    function acf_mbm_before_save_post($post_id) {
    	if (empty($_POST['acf']))
    		return;
    	$_POST['acf']['_post_content'] = $_POST['acf']['field_5bec5fcff9ee0'] ;
    	$_POST['acf']['_post_title'] = $_POST['acf']['field_5bec5e10f9ede'] ;
    	
    	return $post_id;
    }
    add_action('acf/pre_save_post', 'acf_mbm_before_save_post', -1);
    
    function acf_tours_before_save_post($post_id) {
    	if (empty($_POST['acf']))
    		return;
    	$_POST['acf']['_post_content'] = $_POST['acf']['field_5c59be78fa3e0'] ;
    	$_POST['acf']['_post_title'] = $_POST['acf']['field_5c607a4750d32'] ;
    	
    	return $post_id;
    }
    add_action('acf/pre_save_post', 'acf_tours_before_save_post', -1);
    
    function acf_hotel_before_save_post($post_id) {
    	if (empty($_POST['acf']))
    		return;
    	$_POST['acf']['_post_content'] = $_POST['acf']['field_5c6c376283e46'] ;
    	$_POST['acf']['_post_title'] = $_POST['acf']['field_5c6c298e83e40'] ;
    	
    	return $post_id;
    }
    add_action('acf/pre_save_post', 'acf_hotel_before_save_post', -1);
    
    /*
    ********  only this code  work   *******
    */
    function acf_karbar_before_save_post($post_id) {
    	if (empty($_POST['acf']))
    		return;
    		
    	$_POST['acf']['_post_content'] = $_POST['acf']['field_5c6c376283e46'] ;
    	$_POST['acf']['_post_title'] = $_POST['acf']['field_5c6c298e83e41'] ;
    	
    	return $post_id;
    }
    add_action('acf/pre_save_post', 'acf_karbar_before_save_post', -1);
     
    

    How can i merg these codes togather?

  • Hi
    I use this code but at end of the title it add array.
    Any help would be greatly appreciated.

    
    add_action('acf/save_post', 'my_save_acfpost', 20);
    function my_save_acfpost($post_id){
      
      // Get the title from a field
      $karbar_title = get_field('field_5c6c298e83e41', $post_id);
      $hotel_title = get_field('field_5c6c298e83e40', $post_id);
      $tour_title = get_field('field_5c607a4750d32', $post_id);
      $tamas_title = get_field('field_5e0b69b807458', $post_id);
      
      $acfnew_title = $karbar_title . ' ' . $hotel_title . ' '. $tamas_title . ' ' . $tour_title;
      
      
      // Get the content from a field
      $tamas_content = get_field('field_5e0b6b410745b', $post_id);
      $hotel_content = get_field('field_5c6c376283e46', $post_id);
      $tour_content = get_field('field_5c59be78fa3e0', $post_id);
      
      $acfnew_content = $tamas_content . ' ' . $hotel_content . ' '. $tour_content ;
    	
      // Set the post data
      $newacf_post = array(
     
          'ID'           => $post_id,
          'post_title'   => $acfnew_title,
    	  'post_content'	=> $acfnew_content,
      );  
      // Update the post
      wp_update_post( $newacf_post );    
    }
    
    

    null

  • In the first question, you need to define a different post id in each form. for example new_hotel, new_tour and new_karbar. Then in the pre_save_post filter you check for this value and do something different depending on what you want to do. The reason that only your last filter is working is because all of them are running on every post created. All of them are doing what they are supposed to be doing but you’re only seeing the results of the last change.

    As far as the second question, what type of field is causing array. Some fields store array values and you need to account for this.

  • Thanks John Huebner

    The type of field where the array is added is “text”.
    Please give me an example code.
    my codeing is not very good and I am beginner.

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

The topic ‘Using acf field to define a new post content & title’ is closed to new replies.