If you want to Yoast add focus keyword to all posts that don’t already have one, two short SQL statements do it in one bulk operation. They set each post’s Yoast focus keyphrase to its own title wherever the field is missing or empty. It’s the fastest way to clear a dashboard full of red “no focus keyphrase” warnings after a large import, migration, or content audit.
Updated October 2026: the query now also fills keyphrases that exist but are empty, adds a dry run, and includes a RankMath version.
Table of Contents
The SQL Query to Add a Yoast Focus Keyword to All Posts
Yoast SEO stores the focus keyphrase for every post in the wp_postmeta table under the meta key _yoast_wpseo_focuskw. A post can be missing a keyphrase in two ways: there is no row at all, or the row exists with an empty value. The original one-statement version only caught the first case, so run both.
Back up first. Export the database before changing anything:
wp db export before-focus-keywords.sql
Dry run. This counts how many published posts have no keyphrase row, without changing anything:
SELECT COUNT(*)
FROM wp_posts p
LEFT JOIN wp_postmeta pm
ON p.ID = pm.post_id AND pm.meta_key = '_yoast_wpseo_focuskw'
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND pm.post_id IS NULL;
Step 1: posts with no keyphrase row.
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT p.ID, '_yoast_wpseo_focuskw', p.post_title
FROM wp_posts p
LEFT JOIN wp_postmeta pm
ON p.ID = pm.post_id AND pm.meta_key = '_yoast_wpseo_focuskw'
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND pm.post_id IS NULL;
Step 2: posts where the keyphrase exists but is empty.
UPDATE wp_postmeta pm
JOIN wp_posts p ON p.ID = pm.post_id
SET pm.meta_value = p.post_title
WHERE pm.meta_key = '_yoast_wpseo_focuskw'
AND pm.meta_value = ''
AND p.post_type = 'post'
AND p.post_status = 'publish';
How the Query Works
wp_posts p: selects posts from thewp_poststable.LEFT JOIN wp_postmeta pm: joins the focus keyphrase row, if one exists, for each post.pm.post_id IS NULL: keeps only the posts that have no keyphrase row yet, so existing keyphrases are never overwritten.p.post_title: uses the post title as the keyphrase.- Step 2’s
pm.meta_value = '': targets rows that exist but hold an empty string.
How to Run the Query
- phpMyAdmin or Adminer: paste each statement into the SQL tab for your WordPress database and run it.
- WP-CLI: over SSH,
wp db query "PASTE_QUERY_HERE"runs a statement against the active database without a GUI. - Hosting control panel: cPanel, Plesk and most managed hosts expose a database query tool that works like phpMyAdmin.
- Table prefix: if your tables don’t start with
wp_, replace it in every statement.wp db prefixprints the right one.
Using RankMath Instead of Yoast
RankMath keeps its focus keyword in a different meta key, rank_math_focus_keyword. The same pattern works. Swap the key in Step 1:
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT p.ID, 'rank_math_focus_keyword', p.post_title
FROM wp_posts p
LEFT JOIN wp_postmeta pm
ON p.ID = pm.post_id AND pm.meta_key = 'rank_math_focus_keyword'
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND pm.post_id IS NULL;
Why the SEO Score Does Not Update Straight Away
Setting the keyphrase doesn’t re-score the post. Yoast calculates its SEO score inside the editor, so the traffic-light dots in the Posts list only change once a post has been opened and saved. After running the query, go to Posts, filter by “Posts without focus keyphrase”, and the list should be empty or close to it. When I ran this myself, opening and saving a couple of posts was enough for the rest to be picked up, but if scores still look stale, open and save a few of your most important posts first.
Swap Title Keywords for Real Search Terms
A keyphrase copied from the post title is a placeholder, not a keyword strategy. Long titles rarely match what people actually search for. Once the warnings are cleared, replace the title keyphrases on your highest-traffic posts with the top query for each page from Search Console. If you’re also dealing with Yoast and a block theme fighting over the title tag itself, that’s a separate (and more annoying) issue. See my post on duplicate title tags with Yoast SEO and block themes for the fix.
