Receiving a 'previously used keyphrase' SEO assessment on an already deleted term
Original JIRA task (from IM board)
- [x] I've read and understood the contribution guidelines.
- [x] I've searched for any related issues and avoided creating a duplicate issue.
Please give us a description of what happened
By default, if you create a term (e.g., category or tag), it will use the 'name' of the term as the default focus keyphrase. However, if you were to create two terms, and then change the focus keyphrase so that they both share the same focus keyphrase (e.g. Citizens), you would then receive the assessment of:
Previously used keyphrase: You've used this keyphrase once before. Do not use your keyphrase more than once.
Once you delete one of the other terns (there's no 'trash' filter and it mentions permanently deleting), you would expect that the assessment wouldn't appear anymore for the remaining term. But it still shows the same assessment, and when you click on the text link in the 'once before' part of the assessment, it will show you:
You attempted to edit an item that doesn’t exist. Perhaps it was deleted?
You can resolve this for posts by 'permanently deleting' it when going to the 'trash' filter, but there's no option on this for terms. Updating the term, or resetting the data using the Yoast Test Helper plugin doesn't resolve the issue.
To Reproduce
Step-by-step reproduction instructions
- Activate Yoast SEO
- Create a tag, call it
example - Edit
example, confirm the keyphrase isexample - Confirm receive this feedback in the SEO analysis.
Previously used keyphrase: You've not used this keyphrase before, very good.
- Create a tag, call it
duplicate - Edit
duplicate, change the keyphrase fromduplicatetoexample. - Edit
example. - Confirm you receive this feedback in the SEO analysis.
Previously used keyphrase: You've used this keyphrase once before. Do not use your keyphrase more than once.
- Remove
duplicate. - Edit
example. - Confirm you (still) receive this feedback in the SEO analysis.
Previously used keyphrase: You've used this keyphrase once before. Do not use your keyphrase more than once.
- Confirm that clicking on
once beforegives an error message.
Expected results
- After deletion, I expect the feedback for "previously used keyphrase" to disappear.
Actual results
- After deletion, the feedback for "previously used keyphrase" persists.
Screenshots, screen recording, code snippet
See some screenshots in the Jira issue.
Technical info
- If relevant, which editor is affected (or editors):
- [ ] Block Editor
- [ ] Gutenberg Editor
- [ ] Elementor Editor
- [x] Classic Editor (given that's it a term we're editing/deleting)
- [ ] Other:
- Which browser is affected (or browsers):
- [ ] Chrome
- [ ] Firefox
- [ ] Safari
- [ ] Other:
Used versions
- Device you are using: Laptop
- Operating system: Mac OS
- PHP version: 8.1.9
- WordPress version: 6.1.1
- WordPress Theme: -
- Yoast SEO version:
trunk(20.0-RC5) - Relevant plugins in case of a bug: -
After some investigation together with @marinakoleva , this is a technical summary of the culprit:
The source of truth for assessing if a keyword is used elsewhere in the site is the wpseo_taxonomy_meta option (that's being used in the return WPSEO_Taxonomy_Meta::get_keyword_usage( $focuskw, $this->term->term_id, $this->term->taxonomy ); line of the get_focus_keyword_usage() method in WPSEO_Term_Metabox_Formatter.
The issue at hand is that when a term is being deleted, we don't update the wpseo_taxonomy_meta option with this new information, so any keyword usage is not being updated.
A possible/probable solution is to hook into the delete_term action in WPSEO_Taxonomy class (much like we hook into the edit_term action when a term is updated), so that we can update the wpseo_taxonomy_meta option.
Once we do that, then wpseoScriptData.metabox.keyword_usage will contain the updated information even if a term has been deleted.
For the dev that picks this up:
One thing that strikes me as weird is that we have the wpseo_taxonomy_meta option as the source of the truth here, even though that info exists in the terms' indexables as well.
Maybe a good candidate for improvement in the future?