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
totrue
, or this is what is gonna happen.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 toadd_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 likeif (!add_post_meta($post_id,'select_inco',$_POST['select_inco'],true)) {update_post_meta($post_id,'select_inco',$_POST['select_inco'];}