Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 );
  wp_update_post($post);
}

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?

share|improve this question

closed as off-topic by birgire, Pieter Goosen, Johannes Pille, G. M., ialocin Aug 30 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, G. M., ialocin
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
That did it! Thank you! –  Gwendydd Mar 27 '13 at 23:52

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