Set category with a hook
-
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)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Set category with a hook’ is closed to new replies.