I'm passing some data through AJAX to a function in my functions.php, and am trying to save that as metadata to post '101' with a key name "test1". My PHP function is:

function fztest(){

    $datum = $_POST['datum'];
    $post_ID = $_POST['post_ID'];

    add_post_meta($post_ID, 'test1', $datum);

    // And since $datum and $post_ID are correctly returning the values I want, but the meta is not saving, 
    // I've tried troubleshooting by putting some manual operations in various places below:

    add_post_meta(101, 'test2', 'foobar');


    add_post_meta(101, 'test3', 'foobar');

add_action('wp_ajax_foobar', 'fztest');
add_action('wp_ajax_nopriv_foobar', 'fztest');

add_post_meta(101, 'test4', 'foobar');

But when I try to return get_post_meta(101, 'test1'), for example - only 'test4' above works - it just returns an empty string for test1, test2 and test3.

My JS is:

    url: fz.ajax_url,
    type: "POST",
    data: {
        action: "foobar",
        datum: "foobar",
        post_ID: fz.post_ID,
    success: function(response){


... and that is correctly passing the values through to the PHP function, and logging a response when I have tried one.

I can't see why add_post_meta would not work in the PHP function? There's a similar question here where the solution indicates it clearly should and CAN work. How do I update_post_meta() or add_post_meta() with an AJAX call. And their code looks the same as mine as far as I can see.

Can anyone see what I'm doing wrong?

  • there are no strings/quotes around your $_POST is this intentional? My understanding is that's invalid PHP, and $_POST[post_ID]; should be $_POST['post_ID'];. I also see it's reading datum in the AJAX handler, but datum is nowhere to be found in your AJAX code. The add_post_meta for test3 will never run because there's a wp_die just before it. From what I can tell, your PHP error log should be full of warnings and errors from that code. It's also not returning a response so there's no way to get info directly from the AJAX call
    – Tom J Nowell
    May 23 at 14:23
  • Sorry - the datum/subject business was sloppy editing within StackExchange. Have corrected that in edits. Also added in quote marks, although it was processing that ok. I agree that I didn't expect 'test3' to work, but was getting desperate! It's still not making any difference though - any ideas? Thanks! May 23 at 14:31
  • I see, just be mindful that you may have edited out the problem trying to hide your client/production code, have you been able to reproduce the problem using the modified version in your question? How did you confirm the AJAX endpoint is successfully receiving the post ID and data? And you've directly confirmed that fz.post_ID has the expected value? Note that checking if the post meta is present afterwards is an integration test and you can't draw any conclusions beyond wether it worked or didn't work which isn't fine grained enough to answer it
    – Tom J Nowell
    May 23 at 14:40
  • note that in this case update_post_meta should suffice and would be more reliable, but there is nothing wrong with your usage of those functions, the problem is elsewhere, it's more likely the variables being passed into the function don't have the values you think they do. Right now there's no evidence the AJAX endpoint is being called correctly, fz.ajax_url may not have a valid value, there's no response to check, and no way to test for success or failure from JS
    – Tom J Nowell
    May 23 at 14:43
  • I started off building up a bog standard AJAX function step-by-step: checking that fz.post_ID had the correct value; that it was being passed to functions.php, was getting returned to the JS file and was registering via a console.log(response). All worked perfectly. It was only when I tried saving the data that any error was encountered. Have also tried it with update_post_meta, and no cigar either... :/ May 23 at 16:04


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.