How to copy title of WP post dynamically to focus

2019-09-10 03:47发布

I have around 1K posts in my blog. I use Yoast SEO Wordpress Plugin. All my posts doesn't has focus keyword added. Is there any way possible I can add title of the post dynamically to focus keyword field instead of copy paste one by one?

enter image description here

标签: php wordpress
1条回答
叼着烟拽天下
2楼-- · 2019-09-10 04:48

Yoast Focus Keywords is a post meta, you can create a loop that loops through all the posts and then update the post meta with get_the_title();

Sample code:

$title = get_the_title();
$args = array('posts_per_page' => -1, 'post_type' => 'post');
$posts = new WP_Query($args);

foreach($posts as $post){
    update_post_meta($post->ID, '_yoast_wpseo_focuskw', $title);
}

wp_reset_postdata();

You can add the code inside your functions.php or create a page template with the code.

Update:

Create a blank page template that doesn't contain a loop, then add the below code into the file and create a page using this page template.

$posts_query = new WP_Query(
        array(
            'post_type' => 'post',
            'posts_per_page' => -1
        )
    );

    while($posts_query->have_posts()) : $posts_query->the_post();
        $title = get_the_title();
        $post_id = get_the_ID();

        update_post_meta($post_id, '_yoast_wpseo_focusk‌​w', $title);

        echo $title . ' Meta Updated<br />';
    endwhile;

Visit the page using the page template and it should go through each post and display "TITLE Meta Updated" each time it goes through a post.

查看更多
登录 后发表回答