Get values ​​of custom fields in the filter on wp_insert_post_data

Hi everyone , thanks for reading.


Environment :

Wordpress Plugin + Additional Custom Fields


Problem:

I searched for several hours, and I cannot find the correct syntax to do the following:

When you publish a new message, get the value of the custom field to automatically replace the message header with this value. Example: I create a message and set the '10am' to my time 'user field. The message header is automatically replaced with "10 a.m."


Example:

So, I add a filter with the following:

add_filter('wp_insert_post_data', 'change_title') ;

function change_title($data)

{

    $time = XXX ; // How should I get this custom field value ?

    $new_title = 'Topic created at'.$time ;

    $data['post_title'] = $time ;

    return $data;

}

, , WP, . , - , .

!

+6
4

$_POST , , , save_post , :

add_action('save_post', 'change_title');

function change_title($post_id) {
    $time = get_field('time',$post_id);
    $post_title = 'Topic created at '. $time;

    // unhook this function so it doesn't loop infinitely
    remove_action('save_post', 'change_title');

    // update the post, which calls save_post again
    wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));

    // re-hook this function
    add_action('save_post', 'change_title');
}  

, ACF "".

: .

+4

Riadh ( , rep):

WordPress Codex wp_update_post save_post, wp_update_post() save_post . , , :

add_action('save_post', 'change_title');

function change_title($post_id) {
    $time = get_field('time',$post_id);
    $post_title = 'Topic created at '. $time;

    // unhook this function so it doesn't loop infinitely
    remove_action('save_post', 'change_title');

    // update the post, which calls save_post again
    wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));

    // re-hook this function
    add_action('save_post', 'change_title');
}    
+8

add_filter( 'wp_insert_post_data', 'change_title', '99', 2 );

function change_title($data , $postarr){

    $custom_field = 'custom_filed_name';
    $post_id = $postarr['ID'];
    $time = get_post_meta( $post_id, $custom_field, true );

    // Now you have the value, do whatever you want
}
0

" " . . , Wordpress.

data-field-key. , data-field-key="field_5847b00820f13" . $postarr wp_insert_post_data. fields $postarr.

, , / . , PHP, PHP.

post_title $data, .

As a result, the value post_titlewill be saved in the database using the built-in Wordpress logging logic.

add_filter('wp_insert_post_data', 'slb_set_title', '99', 2);

function slb_set_title ($data, $postarr){
  if($data['post_type']==='slb_subscriber'){
    $data['post_title'] = $postarr['fields']['field_5847b00820f13'] .' '.       
    $postarr['fields']['field_5847b03f20f14'];
  }
return $data;
}
0
source

All Articles