According to the Codex, using add_post_meta()
you can’t. That function is supposed to return either true
or false
.
However, that function is a thin wrapper around add_metadata()
…
1726 function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) { 1727 // make sure meta is added to the post, not a revision 1728 if ( $the_post = wp_is_post_revision($post_id) ) 1729 $post_id = $the_post; 1730 1731 return add_metadata('post', $post_id, $meta_key, $meta_value, $unique); 1732 }
https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-includes/post.php#L1726
… and add_metadata()
returns the row insert ID.
Return Values
(boolean|integer)
Returnsfalse
on failure. On success, returns the ID of the inserted row. However, note that the return value may instead be the
result of theadd_{$meta_type}_metadata
filter.
You can see that in the source as well:
73 $mid = (int) $wpdb->insert_id; 74 75 wp_cache_delete($object_id, $meta_type . '_meta'); 76 77 do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value ); 78 79 return $mid; 80 }
https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-includes/meta.php#L31
Given the source to those two functions, I don’t see how add_post_meta()
can return a boolean
when it is just passing information to, and returning the output of, add_metadata()
.
I just did a quick test of add_post_meta()
on an old (3.6) install on a purely local testing server and I got the ID, not true
or false
. If you are getting a boolean
I suspect it might be “the result of the add_{$meta_type}_metadata
filter” as noted in the docs to add_metadata()
The problem could also be related to how your AJAX operates, but you didn’t post any detail, or code, related to that.