I have install plugin Yoast SEO Premium, with this plugin have some field is
yoast_wpseo_metadesc,yoast_wpseo_focuskw
but I can't add althought I used:
add_post_meta($post_id, 'yoast_wpseo_metadesc',$my_post['post_excerpt']);
I have install plugin Yoast SEO Premium, with this plugin have some field is
yoast_wpseo_metadesc,yoast_wpseo_focuskw
but I can't add althought I used:
add_post_meta($post_id, 'yoast_wpseo_metadesc',$my_post['post_excerpt']);
This problem may be due to several concerns, here are some ideas :
Yoast SEO prefix these custom fields entries with a "_" in the database. The key is not "yoast_wpseo_metadesc" but "_yoast_wpseo_metadesc". Same for "yoast_wpseo_focuskw", it's actually "_yoast_wpseo_focuskw".
Add_post_meta() can create a custom field for a post if the field does not exist. If it exists, it does not update it. It's always better to use the update_post_meta() function, more flexible. If the custom field already exists, it will be updated. Otherwise, the function will call add_post_meta() to create it. In your case, a field maybe already exists with that name.
I think we need more information on where you launch your add_post_meta() function. Depending on the context, the approach is different. Here are some examples:
If it's on your single page, you can use :
<?php
global $post;
update_post_meta( $post->ID, '_yoast_wpseo_metadesc', $post->post_excerpt );
update_post_meta( $post->ID, '_yoast_wpseo_focuskw', my_focus_keyword' );
?>
Place this code in functions.php, it do that after each post saving, in admin section.
<?php
// Launch the update_post_meta on post saving.
add_action( 'save_post', 'my_yoast_saved_datas' );
function my_yoast_saved_datas( $post_id, $post ) {
// Check that your post is what you want
if ( $_POST['post_type'] == 'post' ) {
// Check if user can't do that
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
}
update_post_meta( $post_id, '_yoast_wpseo_metadesc', $post->post_excerpt );
update_post_meta( $post_id, '_yoast_wpseo_focuskw', 'my_focus_keyword');
}
?>
Launch one time, in functions.php or pack it on a plugin to launch on activation.
<?php
global $post;
// Array of args, change to your need
$args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'post_status'=> 'publish' );
// Get alls posts
$my_posts = get_posts( $args );
// Loop on post
foreach ( $myposts as $post ) {
setup_postdata( $post );
global $post;
// Update on create custom fields
update_post_meta( $post->ID, '_yoast_wpseo_metadesc', $post->post_excerpt );
update_post_meta( $post->ID, '_yoast_wpseo_focuskw', 'my_focus_keyword');
}
wp_reset_postdata();
?>