0

I am trying to update Yoast post meta to set no index based on an ACF field, but it is not working anymore. Setting an ACF field is way easier for clients than going to the Yoast SEO plugin settings on a post/page.

function px_lp_update_post_meta_based_on_acf_fields( $post_id ) {

if (get_post_meta($post_id, 'lp_hide', true) == 'false') {
delete_post_meta($post_id, '_yoast_wpseo_meta-robots-nofollow');
delete_post_meta($post_id, '_yoast_wpseo_meta-robots-noindex');
}

else {
update_post_meta($post_id, '_yoast_wpseo_meta-robots-nofollow', '1');
update_post_meta($post_id, '_yoast_wpseo_meta-robots-noindex', '1');
}

}
add_action('post_updated', 'px_lp_update_post_meta_based_on_acf_fields', 10);
add_action('save_post', 'px_lp_update_post_meta_based_on_acf_fields', 10);
1
  • Honestly, I'm not a big fan of touching a plugin's options directly. Instead, I'd personally prefer to use an official hook such as wpseo_robots which allows you to use your own logic to change what Yoast thinks it is supposed to do. The bad part is that if someone were to use Yoast's UI and not your custom one, your code might trump it.
    – Chris Haas
    Commented Jan 16 at 14:34

0

Browse other questions tagged or ask your own question.