just trying to reference a specific listing attribute. I created an attribute called “currency” that is a Select field type. The host can then select EUR, or USD, etc…
Let’s say the host selected EUR for his listing.
Now, having the listing object loaded, referencing and displaying $listing->get_currency() does not show the selected value, but instead returns “Array”.
So this field is stored as an array, but how to reference the currently selected value from the array?
Yes, selectable attributes store the selected (or an array) of objects (terms), but you can reference their fields via a double underscore, for example:
$listing->get_currency__name()
It should return EUR if it’s a single selection attribute, if it allows multiple selection then it’ll return an array of currency names.
thanks for the answer, but that didn’t work for me. I am able to reference all the custom attributes, except for those which are select, checkbox or radio types.
The exact name (slug) of the custom attribute I am trying to get displayed is “currency_shown”. It is a Select field with single selection only. The listing is indeed in EUR, but I can’t get it
Tried:
echo $listing → get_currency_shown ();
Shows nothing. I assigned it to a variable and tried to display the variable. Same result, nothing shown. Tried as well (literally):
$listing->get_currency_shown__name()
Nothing shown. I assume I have to put the name of something instead of “name”, but what should I put?
I realized that when doing $currency= $listing->get_currency_shown(), the variable $currency does not get the full array. It remains empty (NULL).
Please try testing this via var_dump first, to check if there’s a value at all (because attempting to output an array will show nothing if WP_DEBUG is disabled). Please try this:
var_dump($listing->get_currency_shown());
If it’s a non-empty array, then this should work in any case: