I am using Gravity Forms to let users create posts. The user fills out a bunch of information, and a post is created. The user gets to choose which categories the post is in, but there is one more category that I need to add for every post. I am trying to use the gform_after_submission hook (http://www.gravityhelp.com/documentation/page/Gform_after_submission) and wp_update_post. I just want to add one more category to the post. Here is my code:

add_action("gform_after_submission_5", "add_category", 10, 2);
function add_category($entry) {
  $post = get_post($entry["post_id"]);
  $post->post_category = array ( 48 );

This does add the appropriate category, so that's good, but it also removes ALL of the other post data, which is bad.

What am I doing wrong? Can this code be made to work, or should I be taking another approach?

closed as off-topic by birgire, Pieter Goosen, Johannes Pille, gmazzap, Nicolai Aug 30 '14 at 11:31

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – birgire, Pieter Goosen, Johannes Pille, gmazzap, Nicolai
If this question can be reworded to fit the rules in the help center, please edit the question.


Looking at core, it seems post_category always overwrites:

 // Passed post category list overwrites existing category list if not empty.

Instead, try something like:

//wp_set_post_terms( $post_id, $terms, $taxonomy, $append )
wp_set_post_terms( $entry["post_id"], 48, $taxonomy, true )

Obviously, replace $taxonomy with the correct name.

Not the answer you're looking for? Browse other questions tagged or ask your own question.