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
add comment

1 Answer

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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