I am making an attempt to avoid wasting submit id and key phrase (Yoast Focus Key phrase) to a customized desk when submit is created or up to date through save_post
motion. It saves every part positive on Publish (first-time) however after I replace the submit, it will get outdated worth for focus key phrase from database and would not save the brand new worth.
for instance
Focus Key phrase (Publish) = "Hello World" (works positive and key phrase is saved correctly)
Focus Key phrase (1st Replace) = "Hello" (Does not work and retains "Hello World" in customized desk)
Focus Key phrase (2nd Replace) = "Hello again" (It saves "Hello")
So principally the issue is that get_post_meta($post_id, ‘_yoast_wpseo_focuskw’, true) returns which is already within the database and never the brand new worth that’s being saved.
What’s one of the simplest ways to get the brand new worth whereas the submit is being saved through save_post
motion. $_POST[‘_yoast_wpseo_focuskw’] will not work as a result of Yoast Focus Key phrase enter area would not have identify set on enter area. Screenshot
Any assist could be appreciated. Thanks
That is my code.
add_action('save_post', 'my_custom_table');
operate my_custom_table($post_id) {
world $wpdb;
$table_name = $wpdb->prefix . "custom_table";
$information = [
'post_id' => $post_id,
'keyword' => get_post_meta( $post_id, '_yoast_wpseo_focuskw', true )
];
//this to preventtwice insert by save_post motion :)
if (outlined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
} else {
//verify if new submit so insert
if( strpos( wp_get_raw_referer(), 'post-new' ) > 0 ) {
if (get_post_status($post_id) === 'publish') {
$wpdb->insert(
$table_name,
$information
);
}
} else {
$wpdb->replace(
$table_name,
$information,
array(
'post_id' => $post_id
),
array( '%d', '%s' ),
array( '%d' )
);
}
}
}