wordpress-seo
wordpress-seo copied to clipboard
wp_set_object_terms doesn't add term into a breadcrumbs.
Here's the example where you can see the bug:
$insert_post_id = wp_insert_post(array(
'post_title'=>$some_title,
'post_content'=>wp_slash($some_content),
'post_status'=>'publish',
'post_type'=>'question',
));
$question_category_insert = wp_set_object_terms($insert_post_id, $question_term, 'question-category');
When I create post programmaticaly and assigning the term to this post - yoast breadcrumbs doesn't show link to a term.
But if I go in admin - selecting newly created post and just pressing "Update" without any changes - the link to term in breadcrumbs appeared.
@Komarovski I wasn't able to reproduce the error, these are the steps I followed:
- I added Yoast bread crumbs to footer via widgets of
Twenty Twenty Onetheme. - I added your code to the single.php
$insert_post_id = wp_insert_post(array(
'post_title'=>$some_title,
'post_content'=>wp_slash($some_content),
'post_status'=>'publish',
'post_type'=>'post',
));
$question_category_insert = wp_set_object_terms($insert_post_id, 'test-category', 'category');
- I then went to the list of posts and viewed the post that was just created.
- Breadcrumbs were correct.
Can you add more details to that errors?
- How do you trigger that code
- If you enable the DEBUG mode and DEBUG_LOG do you get any notices related?
Hey,
I'm implementing breadcrumbs in my theme with yoast_breadcrumb('<p id="breadcrumbs">','</p>'); function in header.php file.
In wp_set_object_terms function I'm using custom taxonomy "question-category" not the native "category" and in wp_insert_post I'm using custom post type "question" not the native "post". I haven't check how it works with native post types and taxonomies.
The debug log is empty.
@Komarovski I wasn't able to reproduce the error, these are the steps I followed:
- I created a custom post type and a matching custom taxonomy as
question. - I added the code to single.php with a term that doesn't exist.
- I added
yoast_breadcrumb('<p id="breadcrumbs">','</p>');toheader.php. - In
Yoast SEO->Settings->Advanced->BreadcrumbsI changed:Breadcrumbs for post types: For my custom post type I setcategoriesBreadcrumbs for taxonomies: For my custom taxonomy I set the custom post typeEnable breadcrumbs for your themeis turned on.
- I viewed one of my previews posts, that triggered
wp_insert_postandwp_set_object_terms - I checked a post and term were created for my custom post type.
- I viewed the new post and checked term in breadcrumbs has a link. So I wasn't able to reproduce...
Hi,
I have the same problem.
Please check here: https://wordpress.org/support/topic/yoast_breadcrumb-problem-2/
You don't need to create a new post type, this bug is on any post type or taxonomy, even on the default one.
Just run this code on a page template: `$post_id = wp_insert_post( array( 'post_type' => 'post', 'post_title' => 'random title', 'post_content' => '', 'post_status' => 'publish' ));
if ($post_id != 0) { wp_set_object_terms($post_id, 'catname', 'category'); }`
Then check the created post page. It outputs something like as:
Home » Uncategorized » random title inside of Home » catname » random title
You should create post from wp_insert_post and not update/change anything on wp-admin (as will fix breadcrumb bug on any update/changes to created post via wp_insert_post on wp-admin).
After some tests, I see this bug happened only from version 19.14 to the latest version of the Yoast SEO plugin.
https://developer.yoast.com/changelog/yoast-seo/19.14/
Any version lower than 19.14 works as expected!
Tested with:
- WordPress: 6.2.2
- Twenty Twenty-ThreeVersion: 1.1
Plugins:
- Yoast SEO
We cannot reproduce the bug anymore, not even with previous versions of Yoast SEO that were reported buggy. We are using WP 6.4 now, all the reports are dated before WP 6.3 was released, so we can't rule out that this had a role in triggering the problem. @Komarovski @balex25 are you still experiencing the issue?
We cannot reproduce the bug anymore, not even with previous versions of Yoast SEO that were reported buggy. We are using WP 6.4 now, all the reports are dated before WP 6.3 was released, so we can't rule out that this had a role in triggering the problem. @Komarovski @balex25 are you still experiencing the issue?
Note: I tested on the latest versions:
- WP 6.4
- Yoast SEO 21.6
Yes, problems are still there. I digged more and found something. Seem the problem comes from:
wp_yoast_indexable
wp_yoast_indexable_hierarchy
So, I created a post via the wp_insert_post function (custom post type "item"), then grabbed the post ID and updated a taxonomy on the created post via wp_set_object_terms.
$post_id = wp_insert_post( array(
'post_type' => 'item',
'post_title' => 'Post Title',
'post_content' => 'Post Content',
'post_status' => 'publish'
));
Now after the post is created and get the post id, I set the custom taxonomy as:
wp_set_object_terms($post_id, 'Term Name', 'origin'); (origin taxonomy is attacked to "item" post type)
Also, I set on "Breadcrumbs for post types" settings:
- Items (item) to show Origin taxonomy in breadcrumb.
Problems:
wp_yoast_indexable:
Now when the post is created via wp_insert_post , it adds a row in wp_yoast_indexable, but has_ancestors set to 0, and when I go to edit the post and click on the "update" button (without any changes) value is updated, so has_ancestors is now 1.
wp_yoast_indexable_hierarchy:
Here are the same as above, values after the post is created via wp_insert_post:
- indexable_id:
ID of wp_yoast_indexable - ancestor_id:
0 - depth:
0 - blog_id:
1
when I click on the update button whiout any changes to the post, values are updated like as:
- indexable_id:
ID of wp_yoast_indexable - ancestor_id:
ANCESTOR_ID like as 13419 - depth:
1 - blog_id:
1
So, it incorrectly updated (or not updated) the ancestor when post-taxonomy was updated via the wp_set_object_terms function. Yoast SEO seems to not see taxonomy updated via function wp_set_object_terms and thinks posts have no taxonomy set (until the post is manually updated).
A current fix seems to trigger Yoast SEO to update its indexable data after I updated the custom taxonomy via wp_set_object_terms. This will generate correct Breadcrumbs and have correct ancestors in Yoast tables.
// Update taxonomy
wp_set_object_terms($post_id, $namespace, 'origin');
// Trigger Yoast SEO to update its indexable data
if (function_exists('YoastSEO')) {
// Retrieve the Indexable for the post
$indexable_repository = YoastSEO()->classes->get('Yoast\WP\SEO\Repositories\Indexable_Repository');
$indexable = $indexable_repository->find_by_id_and_type($post_id, 'post', true);
// If an Indexable is found or created, build its hierarchy
if ($indexable) {
$indexable_hierarchy_builder = YoastSEO()->classes->get('Yoast\WP\SEO\Builders\Indexable_Hierarchy_Builder');
$indexable_hierarchy_builder->build($indexable);
}
}
So, the working solution so far for me is as:
- Created post via
wp_insert_post. - Attack a custom taxonomy to the created post.
- Trigger Yoast SEO to update indexable data.
You can easily implement a solution to detect when a post taxonomy is created or updated via hooks like set_object_terms, added_term_relationship, and regenerated post indexable, hierarchy, etc.
@balex25 thanks for the useful info! One thing that might be relevant: how do you trigger the post creation? Is it fired via a hook, and if so, which one? Or are you using any other way (a plugin adding a button, etc.)?
Just to be sure that we are not facing any timing/priority issue there.
Hi @balex25
I hope you are doing great. As we haven't heard back from you, can you please share more details asked by our development team above ? It would be helpful for us to replicate it and to narrow down the issue further.
Looking forward to hearing from you.