Ask your WordPress questions! Pay money and get answers fast! (more info)

How can I insert taxonomy values for a post using a SQL query? WordPress


I have a frontend post form (using wp_insert_post) for my WordPress site, but I am unable to insert ID numbers for custom taxonomies. I have posted on several forums and read a dozen tutorials and for the life of me I can't figure out what's the problem.

So I'd like to directly insert the custom taxonomy values using a SQL query. I just need someone to provide a query I could use to insert an array of taxonomy values into a for a specific post ID.


Answers (2)


Arnav Joy answers:

why you are not using this function ?

<?php wp_set_post_terms( $post_id, $terms, $taxonomy) ?>

you can read more about it here


John Cotton answers:

wp_set_object_terms( $post_id, array_map( 'intval', (array) $_POST['terms] ), 'taxonomy', true );

will do it, assuming you have an array of term ids being posted back. The array_map is important to ensure that ids are seen as numbers not strings - strings will end up creating new terms with a number as their name.

booruguru comments:

I tried your code (even after fixing the typo--the missing quotation mark for $_POST['terms']) and it didn't work. I reworked the code since it seems $_POST should be enclosed within the array parentheses...and it didn't work, I tried using that code fragment above and below the line $pid = wp_insert_post($post); (and of course I replaced the placeholder values as required including $post_id)... and it didn't work.

I just need a SQL query I can use to resolve this problem. I know this is not how you're supposed to use WordPress, but I have no choice.