Write automatic title at save_post (infinite loop)

There is a simple way, You need to use filter hook wp_insert_post_data So the code should be like add_filter( ‘wp_insert_post_data’, ‘set_post_title_with_field_value’ ); function set_post_title_with_field_value( $data ) { if ($data[‘post_type’] == ‘sentence’ ){ $sentence_number = get_field(‘sentencia_no’, $data[‘ID’]); $data[‘post_title’] = $sentence_number; } return $data; } You need to add this code into theme’s functions.php. So try the … Read more

Confusion regarding Nonce & using it in Custom Columns for Saving Checkbox Value to Post Meta

When trying to create a custom metabox, adding custom columns, managing custom columns and finally saving the post. Where do we need to pass the nonce. To make my question clearer. This is not correct, you shouldn’t need a nonce in a metabox because all that information is sent at once in the full request … Read more

Execute function after a post has been published

function check_meta( $post_id, $post, $update ) { $post_type = get_post_type($post_id); if ( “post” != $post_type ) return; $video_code_val = get_post_meta( $post_id, ‘video_code’, true ); // Check if the video_code field has a value. if ( ! empty( $video_code_val ) ){ //if it does, set post format set_post_format( $post_id, ‘video’ ); } } add_action( ‘save_post’, ‘check_meta’, … Read more

Metabox saves on Update or Publish, but not on Saving Draft

So you first tried hooking on to wp_insert_post_data and could save the meta data when saving Drafts but not when publishing. Then you tried hooking on to save_post and could save meta data when publishing but not when saving Drafts. The easiest solution would be to hook on to both. add_action(‘save_post’, ‘save_details’); add_action(‘wp_insert_post_data’, ‘save_details’); Edit … Read more

Undefined ‘post_type’ error on Add new page

save_post is called when post is inserted or updated. When you access add new post a post is created and inserted into database. That is actually draft. At that time $_POST is blank therefore, you are seeing warnings. Solution: The best option is exit the function as soon as you don’t find the nonce. And … Read more

Call add_action() in function wordpress

You’re thinking about this entirely wrong. A meta tag isn’t something you add when a post gets saved, it’s something that gets added to the output when a post is viewed. So instead of trying to hook the action inside save_post, you hook it on every page load, and inside the hook you check if … Read more