WP post_updated $post_after, $post_before showing same product categories

710 views Asked by At

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

There are 3 answers

0
RomkaLTU On BEST ANSWER

Maybe if you can't determine the difference, use this apreach:

update_post_meta($post_ID, 'is_feed', 1)

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.

0
user14788417 On

Yes @RomkaLTU ! Thx a lot that worked for me, here is what I ended up with:

   if ( get_post_meta($post_ID, 'is_feed', true) == 1 || has_term( 1467, 'product_cat', $post_after )) {
        create_gebraucht_feed(true);
        
        if(!has_term( 1467, 'product_cat', $post_after )) {
            update_post_meta($post_ID, 'is_feed', 0);
        }
        return;
    }
0
J. Mann On

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 the pre_post_update-hook like this:

function myFunctionBeforeUpdate($post_id, $data)  {
    //$post_id corresponds to the post BEFORE the update
    //$_POST holds all the information for the post AFTER the update
    
    $old_cats = get_the_category($post_ID);
    $new_cat_ids = ($_POST["post_category"]);
    
    $old_cat_names = array();
    $new_cat_names = array();
    
    foreach ( $old_cats as $category ) {
        $old_cat_names[] = $category->name;
    }
    
    foreach ( $new_cat_ids as $cat_id ) {
        $cat = get_category( $cat_id );
        $new_cat_names[] = $cat->name;
    }

    // you can now use the arrays $old_cat_names and $new_cat_names 
    // which contain the names (not IDs) of the old and new categories


}
add_action('pre_post_update', 'myFunctionBeforeUpdate', 10, 2 );