0

I tried using the function get_post_meta() to get the meta values of my posts. However to my surprise it returns an Array that contains again arrays! So I looked inside the second level array, and was able to get a list of meta values with this code:

$postmeta = get_post_meta($resultid, '', true);

foreach ($postmeta as $meta_key => $v) {
    foreach ($v as $kk => $meta_value) {
        echo "$meta_key => $meta_value.\n";
    }
}

However, even though I put $single = true reading the documentation here: https://developer.wordpress.org/reference/functions/get_post_meta/
It still gives me 2 values for the same meta key.

E.g. I get this list printed:

select_inco => fob.
select_inco => fob.
price_input_currency => usd.
price_input_currency => usd.
price_input => 58.
price_input_to => .
price_per => .
price_comments => 

My Question:

How do I know what the difference is between these double values like select_inco and price_input_currency and which one is the correct one?

This is how I add and also edit meta_values from a form:

update_post_meta($post_id, 'select_inco', $_POST['select_inco']);
  • get_post_meta is not your problem... check in your code where you are usingadd_post_meta that you also set $unique to true, or this is what is gonna happen. – majick Feb 26 '16 at 5:49
  • Thanks @majick! I use this code to save my data from a form. The form can be used to either save or edit the metadata of a post, but in both cases I use this query for all fields of the metadata I want to save. → e.g.: update_post_meta($post_id, 'select_inco', $_POST['select_inco']); – mesqueeb Feb 26 '16 at 8:22
  • I don't see how this can be it, because update_post_meta only works if the meta value already exists. If you have multiple values for the same key, I still think it has to be due to add_post_meta... in contrast, add_post_meta will fail if you set you unique to true and a value already exists. this is why generally best practice is to use something like if (!add_post_meta($post_id,'select_inco',$_POST['select_inco'],true)) {update_post_meta($post_id,'select_inco',$_POST['select_inco'];} – majick Feb 27 '16 at 11:36
  • Aww, thanks majick, but as it says in the documentation for update_post_meta: "This may be used in place of add_post_meta() function. The first thing this function will do is make sure that $meta_key already exists on $post_id. If it does not, add_post_meta($post_id, $meta_key, $meta_value) is called instead and its result is returned." – mesqueeb Feb 27 '16 at 15:15
  • wow, sorry, my bad, they must have changed that behaviour. I am not sure it is actually a better idea than having it only update if the value already exists, but guess that's just how it is now. :-/ – majick Feb 28 '16 at 3:06
0

get_post_meta() is simply just a wrapper for get_metadata(). If we look at the source or get_metadata(), we will find that, if $key is left empty, $single is ignored because of the following lines of code which comes before $single is applied

if ( ! $meta_key ) {
    return $meta_cache;
}

So, in your usecase, $single never gets to do it's job.

The double key=>value pairs simply means that you have an array of values for that specific key, and the two values in the array are the same value. This is caused by a flaw somewhere in your code which is responsible for saving data to your custom field. You should go and search for that particular piece of code and sort that particular issue.

I cannot tell you which is the correct value and which not, that is something that you also cannot answer. As I said, you will need to sort your code responsible for saving your custom field data.

In the meantime, you can break out of your foreach loop to only get the first value from a custom field, but I do not recommend this as a solution, the solution will involve to sort how you save your custom field data

$postmeta = get_post_meta($resultid, '', true);

foreach ($postmeta as $meta_key => $v) {
    foreach ($v as $kk => $meta_value) {
        echo "$meta_key => $meta_value.\n";
        break;
    }
}
  • Dear @pieter-goosen , Thanks! I use this code to save my data from a form. The form can be used to either save or edit the metadata of a post, but in both cases I use this query for all fields of the metadata I want to save. → e.g.: update_post_meta($post_id, 'select_inco', $_POST['select_inco']); – mesqueeb Feb 26 '16 at 8:21
  • Please file an edit to your question with your code, your code is unreadable in comments – Pieter Goosen Feb 26 '16 at 8:25
  • Dear Pieter! I just found out that update_post_meta has a optional field: "$prev_value" if not used it updates all of the meta_values with what I put in the POST data... This means it wouldn't matter if I get the first or second value inside my double-nested-for each right? – mesqueeb Feb 26 '16 at 8:26

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.