PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Save_post function not firing on user-submitted posts

  1. moreslack

    Hey there,
    I have a similar issue as the open request at:

    We have added an action to the "save_post" action to update the post category based on values saved on two custom post fields (we need to support two different searching methods, hence the duplication of info on a single post). The code below works when we save a post through the admin UI:

    function update_category($post_id ) {
    $cat_ids = array( get_post_meta($post_id, place_type, true) , get_post_meta($post_id, location, true) );
    wp_set_object_terms( $post_id, $cat_ids, 'category');
    add_action('save_post', 'update_category');

    However, when the post is created through the Gravity Form submission, the custom fields are populated based on two questions mapped to those two fields, but the corresponding categories aren't updated. Opening the post as an admin and simply click "Update" without applying any changes causes the action to fire. Any idea what we might be doing wrong here?


    Posted 11 years ago on Sunday July 29, 2012 | Permalink
  2. moreslack

    Nevermind. Looks like the solution was to use gform_post_submission instead of save_post. My guess is that the default setting for the post was overriding the save_post action. The following did the trick

    add_action("gform_post_submission", "set_post_categories", 10, 2);
    function set_post_categories($entry, $form){
       //getting post
        $post = get_post($entry["post_id"]);
       //$entry[11] and $entry[12] correspond to the two custom fields that we're mapping to categories
        $cat_ids = array($entry[11],  $entry[12]);
        wp_set_object_terms( $post_id, $cat_ids, 'category');
       //updating post
    Posted 11 years ago on Sunday July 29, 2012 | Permalink
  3. Thank you for the update. Glad that worked for you.

    Posted 11 years ago on Monday July 30, 2012 | Permalink