1

i am having problems printing a specific value stored in a post-meta array in a WordPress loop created by a Visual Composer Grid Builder, thru a shortcode (i also tried the grid builder Custom Field, but wasn't able to find ANY examples).

I tried adding this shortcode in functions.php:

function dmc_prezzo()
{
$postid = get_the_ID(); // get id from post in loop
$meta_data = get_post_meta($postid,'listing_options'); // select array
return $meta_data['price:value'] . ' ' . $meta_data['price:original']; // extract keys
}
add_shortcode('dmcprezzo', 'dmc_prezzo');  //usage [dmcprezzo]

I then added the shortcode to the text-field in the VC Grid template but it is outputting nothing!

Here is the target post meta array, extracted from a post, using DPM plugin:

    listing_options: s:349:"a:8:{s:5:"price";a:2:{s:5:"value";s:5:"17300";s:8:"original";s:5:"19800";}s:17:"custom_tax_inside";s:0:"";s:15:"custom_tax_page";s:0:"";s:8:"city_mpg";a:1:{s:5:"value";s:3:"5.7";}s:11:"highway_mpg";a:1:{s:5:"value";s:3:"4.6";}s:12:"custom_badge";s:7:"Venduta";s:5:"video";s:43:"https://www.youtube.com/watch?v=IBCOAumh6aI";s:10:"short_desc";s:0:"";}";

I can see that the variable containing the array is called $listing_options but i do not know to set the code right.

The values in which i am interested are the price:value and price:original found in the beginning of the array:

s:349:"a:8:{s:5:"price";a:2:{s:5:"value";s:5:"17300";s:8:"original";s:5:"19800";}

Can anyone help please?

1 Answer 1

0

This is the output of your array, when unserialized:

Array
(
    [0] => Array
        (
            [price] => Array
                (
                    [value] => 17300
                    [original] => 19800
                )

            [custom_tax_inside] => 
            [custom_tax_page] => 
            [city_mpg] => Array
                (
                    [value] => 5.7
                )

            [highway_mpg] => Array
                (
                    [value] => 4.6
                )

            [custom_badge] => Venduta
            [video] => https://www.youtube.com/watch?v=IBCOAumh6aI
            [short_desc] => 
        )

)

So, to access the price, you have to change your shortcode to this:

function dmc_prezzo() {
    $postid = get_the_ID(); // get id from post in loop
    $meta_data = get_post_meta($postid,'listing_options'); // select array
    return $meta_data['listing_options']['price']['value'] . ' ' . $meta_data['listing_options']['price']['original']; // extract keys
}
add_shortcode('dmcprezzo', 'dmc_prezzo');  //usage [dmcprezzo]

It would be better if you've posted the entire serialized data, I'm not sure if it's not nested.

0

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.