I’m trying to automatically update an xml feed after I update a product of a certain product category…
I found out I can do that with the post_updated hook.
The feed only contains products of category "Willhaben". So whenever I remove the category "Willhaben" from a product, I need the feed to be updated in order to keep the feed up to date…
My problem is that after I remove the category from my feed the post_updated hook doesn’t trigger anymore, as I added an if in order to not update the feed when a product without the category "Willhaben" gets updated, to avoid overload.
I tried using $post_after, $post_before to check if the product once had the category "Willhaben" and then rebuild the feed, but $post_after, $post_before always give me the exact same list of categories for that specific product…
Here is my code:
function wpdocs_run_on_transition_only( $post_ID, $post_after, $post_before ) {
if(has_term( 1467, 'product_cat', $post_before ) || has_term( 1467, 'product_cat', $post_after)) {
create_gebraucht_feed(true);
return;
}
}
add_action( 'post_updated', 'wpdocs_run_on_transition_only', 10, 3 );
So because the category list is always the same, I can’t determine if the product had the category "Willhaben" and therefore the feed doesn’t get created…
I hope it’s clear what I mean… does anyone here have an idea what I’m doing wrong? I’m facing this issue for hours now and don’t know what to do anymore…
Thx a lot for your time, I’d appreciate some help, thank you!
3
Answers
Yes @RomkaLTU ! Thx a lot that worked for me, here is what I ended up with:
Maybe if you can’t determine the difference, use this apreach:
So if you update a post and it doesn’t contain the required category update is_feed to 0.
update_post_meta
should be at the end of the function. Check It at the beginning.I had the same problem: the variables
$post_before
and$post_after
had the same category although I updated the categories while saving.I have read somewhere else that the
post_updated
-hook runs too late so that both variables hold the same category. I ended up using thepre_post_update
-hook like this: