taxonomy – wp_set_object_terms creates taxonomies but does not add custom posts to it


I am trying to call wp_set_object_terms in a function hooked to custom post creation (via wp_insert_post_data) to set taxonomies tags for the post from a custom field:

$item_brand = ( ! empty( $_POST[ 'brand' ] ) ) ? $_POST[ 'brand' ] : get_post_meta( $postarr[ 'ID' ], 'brand', true );
wp_set_object_terms( $postarr[ 'ID' ], $item_brand, 'brand', false);

The function has no issues creating the taxonomy tags from the custom field when the post is created or modified, but it does not set the terms for the post. My guess is that it is an issue processing the post ID variable, as I tried to set the a static post ID and the post with the static ID was assigned the new tags. So I tried this to mitigate any issues with parsing the current ID:

$post_id = intval ((! empty( $_POST[ 'ID' ] ) ) ? $_POST[ 'ID' ] : $postarr[ 'ID' ]);
wp_set_object_terms( $post_id, $item_brand, 'brand', false);

but it did not help, I also know that I am successfully getting the post ID with the above as I am using it for other purposes within the function, its just not working for wp_set_object_terms. Honestly not sure where to look further at this point and would appreciate any suggestions that might help.

user205912 1 month 2021-05-04T16:59:28-05:00 0 Answers 0 views 0

Leave an answer