• Hi,

    In previous versions (5.x) of WordPress, I can remove categories via ‘wp_insert_post’ hook and ‘wp_set_post_terms’ function for a post when it is published.

    But for a while it didn’t work anymore

    My code is :

    function update_post_terms_image($post_id) {
     if ($parent = wp_is_post_revision($post_id)) {
      $post_id = $parent;
     }
     $post = get_post($post_id);
     if ($post->post_type != 'post') {
      return;
     }
     $user = user_role();
     if (count($user) > 0) {
      if ($user["role"] == 2) {
       // Terms
       $in_category = category_list("in");
         // Returns an array with one integer value that is the only one category to be set ! for example Array([0] => 153)
       wp_set_post_terms($post_id, $in_category, 'category');
    
       // Image
       $images = array("strasbourg"=>4070,
           "rennes"=>4133,
           [...]
           "national"=>9529);
       set_post_thumbnail($post_id, $images[$user["aca_code"]]);
      }
     }
    }
    add_action('wp_insert_post', 'update_post_terms_image');

    The ‘set_post_thumbnail’ function fires without any problem but my post has always multiple categories when I look in categories column in post list.

    Is it normal ?
    What’s wrong ?

Viewing 4 replies - 1 through 4 (of 4 total)
  • What are the functions user_role() and category_list()? They to not exist in WordPress actually.

    Thread Starter pwozniak89

    (@pwozniak89)

    They are mine, user_role returns 2 in that case and category_list returns an array of integer value such as Array([0] => 153) depending of user (every user has his own category, but they frequently forget to select their category ! That’s why I need to set category programmatically).
    These functions work as if I create a new post which had no selected category, it adds category with good id (such as 153 in my example) to the new post.
    But if I create a new draft from an existing post which had another category (for example 122), when I publish this post the old category (122) stills and the correct one (153) isn’t set…

    Moderator bcworkz

    (@bcworkz)

    If the terms you are getting are the one’s currently being set as part of the publish event, you’re likely encountering a race condition where the action fires before SQL is able to write the data into the DB. You cannot reliably get data currently being saved out of the DB during wp_insert_post. You need to intercept the data before it gets written if you wish to use it your code’s logic.

    Intercepting term data has become more difficult with the block editor because it’s saved through the REST API and is no longer included in the post’s $_POST data. You’re better off using hooks specifically for taxonomy terms like “update_category_metadata”. But then the filter is disassociated from the current post. Your callback alone cannot be sure if it’s working on the post from wp_insert_post or something else.

    What you can do is only add the callback when the conditions are correct. However, ‘wp_insert_post’ is too late. You need something early, like ‘wp_insert_post_empty_content’. It doesn’t matter what the filter is for, you are using it like an early action. Then have your “update_category_metadata” callback remove itself from the filter stack so it would not be used for other calls to update_metadata().

    Thread Starter pwozniak89

    (@pwozniak89)

    add_filter(‘wp_insert_post_empty_content’, …) doesn’t work anymore : category KO and image OK, as with add_action(‘wp_insert_post’, …)

    So I write myself to the database after wp_set_post_categories($post_id, $in_category) !
    DELETE FROM wp_term_relationships WHERE object_id = $post_id AND term_taxonomy_id <> $in;

    Dirty but effective !

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Set category with a hook’ is closed to new replies.