How To Change Or Insert WordPress Yoast SEO Fields Programmatically

I have a WordPress site and installed Yoast SEO plugin. I would like to know how can I update or insert Yoast SEO fields when I create a post or page programmatically

Yoast SEO is storing meta title and meta description via post meta. You can use get_post_meta function to retrieve the Yoast meta title or meta description fields and use add_post_meta or update_post_meta to insert or update the Yoast meta title or meta description

Insert Yoast meta title / description

add_post_meta( 1, '_yoast_wpseo_metadesc', "Meta desc");
add_post_meta( 1, '_yoast_wpseo_title', "Meta title");

Update Yoast meta title / description

update_post_meta( 1, '_yoast_wpseo_metadesc', 'Meta desc' );
update_post_meta( 1, '_yoast_wpseo_title', 'Meta title' );

Get Yoast meta title / description

get_post_meta(1, '_yoast_wpseo_metadesc', true);
get_post_meta(1, '_yoast_wpseo_title', true);
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments