0

So I'm working on a custom save-post filter. It's working to handle the external API calls I'm using to generate information, which is super awesome. In total, I'm grabbing file info from an API, downloading a couple of files, writing them to a directory, zipping them and deleting them. It all works. During this process I'm extracting data points from the API stuff and storing them as variables to be saved as meta fields. My function looks like this, minus all of the stuff you don't need to see (assume that the function works EXCEPT update_post_meta() isn't firing).

function stuff_save( $post_id ) {
    $post_type = get_post_type($post_id);
    // If this is just a revision, do nothing.
    if ( wp_is_post_revision( $post_id )|| "animations" != $post_type  )
        return;
   /**code to generate data points**/
        $datapoint = get_post_meta($post_id,custom_field,1);
        $update_var= string_from_API_Call; 
/**uses $datapoint to do a thing; I'm using this result elsewhere, can echo it, and it's successful.**/

        $hat = update_post_meta( $post_id, 'field_name', $update_var );//returns a value, but that number doesn't correspond with any meta key in my database nor does the data save anywhere. 
 }
}add_action( 'save_post', 'stuff_save' );

Now if I echo $hat it returns a number, like it would - but that meta key not only doesn't exist when I look into my post_meta table, but it increments when I refresh the script just the same.

Little help overflowvians?

  • What do you get if after $hat you add echo get_metadata_by_mid( 'post', $hat )? – czerspalace Oct 22 '16 at 0:12
  • Actually I get what I expect to get object(stdClass)#5889 (4) { ["meta_id"]=> string(3) "297" ["post_id"]=> string(2) "post_id" ["meta_key"]=> string(9) "field_name" ["meta_value"]=> string(79) "update_var" } But that value 297 doesn't exist in my database as any type of meta id, nor can I search the entire database for the string. – BlueDreaming Oct 22 '16 at 0:18
  • This doesn't really make any sense, especially if it's returning a meta ID, which means it is adding it, but for some reason if you're saying it doesn't exist in the DB, something else is removing it. I recommend setting up PHPStorm with xdebug which will help you debug this very easily – sMyles Oct 22 '16 at 0:56
0

I think the main problem is in the if block. It does return before executing update_post_meta( $post_id, 'field_name', $update_var ) or it does not executes, cause the indentation. So I think the below code will do your job-

function stuff_save( $post_id ) {

    $post_type = get_post_type($post_id);

    // If this is just a revision, do nothing.
    if ( wp_is_post_revision( $post_id )|| "animations" != $post_type  ) {
        return;   
    }

    /**code to generate data points**/
    $datapoint = get_post_meta($post_id,custom_field,1);
    $update_var= string_from_API_Call;

    /**uses $datapoint to do a thing; I'm using this result elsewhere, can echo it, and it's successful.**/    
    $hat = update_post_meta( $post_id, 'field_name', $update_var );//returns a value, but that number doesn't correspond with any meta key in my database nor does the data save anywhere.

}

add_action( 'save_post', 'stuff_save' );
0

I solved my own problem.

save-post hooks in before the form is saved. It even gives us access to the $_POST object and allows us to interact with it before the original save happens.

What this means essentially is that I'm overwriting my values with blank formdata.

Rather than updating post meta, I've documented and am writing to the $_POST object the updated values, which are then submitted as part of the original save post.

$hat = update_post_meta( $post_id, 'field_name', $update_var );

Becomes

/**document  this well or you'll drive someone insane in the future.**/ 


$_POST['field_name'] = $update_var;

Thanks everyone for your thoughts and attempts to help. You no doubt inspired me to think about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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