categories – Adding category to existing post (uploaded file)


Brand new here and new to WP, running 6.1.1.

I uploaded a file that appears in Media Library. What I want to do is programmatically assign multiple categories to the post after it is uploaded. I have the post ID of the uploaded file and am executing the following code using the Code Snippet plugin as a test in order to assign a single category. When editing the category, the tag_ID is 8 and the URL shows taxonomy=category:

function test_category_update() {
$post_id = 15937;
$uploadCategory = array(8);
wp_set_object_terms( $post_id, $uploadCategory,'category');

But it’s not assigning a category to the upload/post. Am I using the right function?

Lando 3 weeks 2023-03-04T21:13:28-05:00 0 Answers 0 views 0

Leave an answer