I am trying to update the field when post status changes to published. It is getting the values properly but not updating when I check the what update field is returning.
It shows some number.
Please help not sure whether I should use this along with transition_post_status
Update - I have tried below hooks // the action is getting triggered but the fields are not updating
1. publish //works but no update is happening
2. save_post && save_post_l //works but no update is happening.
add_action('transition_post_status', 'updated_to_publish', 10, 3);
function updated_to_publish($new_status, $old_status, $post)
{
if ($new_status == 'publish')) // I have removed the check for post type
{
$post_id = $post->ID;
if (get_field('advanced_option_edit_seo', $post_id)) {
if (defined('WPSEO_VERSION')) {
$metatitle = get_field('seo_title', $post_id);
$metadesc = get_field('seo_meta_description', $post_id);
$metakeywords = get_field('seo_keyword', $post_id);
update_post_meta($post_id, '_yoast_wpseo_title', $metatitle );
update_post_meta($post_id, '_yoast_wpseo_metadesc', $metadesc );
update_post_meta($post_id, '_yoast_wpseo_focuskw', $metakeywords);
}
}
} else {
return;
}
}
Update
I was able to solve the issue by using 'wp_insert_post
' action . Can I know why other actions failed but 'wp_insert_post
' worked?
code that i used for testing
add_action('transition_post_status', 'updated_to_publish', 10, 3);
function updated_to_publish($new_status, $old_status, $post)
{
if (($new_status == 'publish') && ($post->post_type == 'l')) {
$post_id = $post->ID;
error_log( var_export( $post_id, 1 ) );
if (get_field('advanced_option_edit_seo', $post_id)) {
if (defined('WPSEO_VERSION')) {
// ACF field
$metatitle = get_field('seo_title', $post_id);
error_log( var_export( $metatitle, 1 ) );
$metadesc = get_field('seo_meta_description', $post_id);
error_log( var_export( $metadesc, 1 ) );
$metakeywords = get_field('seo_keyword', $post_id);
error_log( var_export( $metakeywords, 1 ) );
//plugin is activated
//old values
$metadesc_old = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
error_log( var_export( $metadesc_old, 1 ) );
$metatitle_old = get_post_meta($post->ID, '_yoast_wpseo_title', true);
error_log( var_export( $metatitle_old, 1 ) );
$metakeywords_old = get_post_meta($post->ID, '_yoast_wpseo_focuskw', true);
error_log( var_export( $metakeywords_old, 1 ) );
update_post_meta($post_id, '_yoast_wpseo_title', $metatitle, $metatitle_old);
error_log( var_export( $tyone, 1 ) );
update_post_meta($post_id, '_yoast_wpseo_metadesc', $metadesc, $metadesc_old);
error_log( var_export( $tytwo, 1 ) );
update_post_meta($post_id, '_yoast_wpseo_focuskw', $metakeywords, $metakeywords_old);
error_log( var_export( $tythree, 1 ) );
}
}
} else {
return;
}
//Do something
}
update_post_meta()
part, then some possible causes are: a) the fields are already updated (i.e. new values equal the current values in the database, soupdate_post_meta()
will update nothing); or b) a plugin/code is changing back (or removing) the field values. With thewp_insert_post
action, the fields may have yet existed in the post being created/updated; hence that's likely why theupdate_post_meta()
calls work.update_post_meta( $post_id, 'test20181114', 'Test 20181114' );
- and see if the field is being added to the post. If it is, then your code works. PS: In my previous comment, I meant to say "When you tested with thewp_insert_post
action".