skip to Main Content

I’m making a wordpres website. i try to get a value from mysql DB. Actually I managed to get a lot of value. but this one so complicate.
For Example; there is a value in DB. its meta_key => ‘th_room_price’ and meta_value => ‘499’ Then i create a shortcode with meta_key and get meta_value.

But now There is another very complicated value. its meta_key => mphb_season_prices and meta_value =>(it is below)
There are 12 separate prices here and i dont know how can i get each price separately. Is there someone who can help?

these are the values of a villa in the DB;

('meta_id' => '3307','post_id' => '426','meta_key' => 'mphb_season_prices','meta_value' => 'a:12:{i:0;a:2:{s:6:"season";s:3:"252";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:422;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:1;a:2:{s:6:"season";s:3:"253";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:422;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:2;a:2:{s:6:"season";s:3:"254";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:422;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:3;a:2:{s:6:"season";s:3:"255";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:493;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:4;a:2:{s:6:"season";s:3:"256";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:493;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:5;a:2:{s:6:"season";s:3:"257";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:708;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:6;a:2:{s:6:"season";s:3:"258";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:893;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:7;a:2:{s:6:"season";s:3:"259";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:893;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:8;a:2:{s:6:"season";s:3:"260";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:708;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:9;a:2:{s:6:"season";s:3:"261";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:565;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:10;a:2:{s:6:"season";s:3:"262";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:465;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}i:11;a:2:{s:6:"season";s:3:"263";s:5:"price";a:4:{s:7:"periods";a:1:{i:0;i:1;}s:6:"prices";a:1:{i:0;d:422;}s:17:"enable_variations";b:0;s:10:"variations";a:0:{}}}}')

By the way this is one of my shortcodes;

function fiyat_shortcode_function() {
$cekilen_deger = get_post_meta( get_the_ID(), 'th_room_price', true );
$var = $cekilen_deger;
return $var;
}
add_shortcode( 'villa_fiyat', 'fiyat_shortcode_function' );

EDIT- final code; (still not working)

function fiyatlar_shortcode_function() {
$var    = get_post_meta( get_the_ID(), 'mphb_season_prices', true);
$var    = unserialize($var);
$season = 252; // 252 january, 253 february, 254 march...

   $foo = array_column($var, 'price', 'season');

}          

print_r($foo[$season]['prices'][0]);

add_shortcode( 'fiyat_tablosu', 'fiyatlar_shortcode_function' );

–Edit 4

function fiyatlar_shortcode_function() {
$var    = get_post_meta( get_the_ID(), 'mphb_season_prices', true);
$var    = unserialize($var);

return $var; 
}

add_shortcode( 'fiyat_tablosu', 'fiyatlar_shortcode_function' );

–Edit 5

function fiyatlar_shortcode_function() {
$va_form = get_post_meta( get_the_ID(), 'mphb_season_prices');
var_dump($va_form);
}
add_shortcode( 'fiyat_tablosu', 'fiyatlar_shortcode_function' );

And Edit 5 Output: array(0){}

2

Answers


  1. Chosen as BEST ANSWER

    code I'm currently using;

    function fiyatlar_shortcode_function() {
    $post_id = 1182;
    $var = get_post_meta( $post_id, 'mphb_season_prices' );
    var_dump($var);
    }
    
    add_shortcode( 'fiyat_tablosu', 'fiyatlar_shortcode_function' );
    

    and output;

    array(1) { [0]=> array(12) { [0]=> array(2) { ["season"]=> string(3) "252" ["price"]=> array(4) { ["periods"]=> array(1) { [0]=> int(1) } ["prices"]=> array(1) { [0]=> float(1070) } ["enable_variations"]=> bool(false) ["variations"]=> array(0) { } } } [1]=> array(2) { ["season"]=> string(3) "253" ["price"]=> array(4) { ["periods"]=> array(1) { [0]=> int(1) } ["prices"]=> array(1) { [0]=> float(1070) } ["enable_variations"]=> bool(false) ["variations"]=> array(0) { } } } [2]=> array(2) { ["season"]=> string(3) "254" ["price"]=> array(4) { ["periods"]=> array(1) { [0]=> int(1) } ["prices"]=> array(1) { [0]=> float(1070) } ["enable_variations"]=> bool(false) ["variations"]=> array(0) { } } } [3]=> array(2) { ["season"]=> string(3) "255" ["price"]=> array(4) { ["periods"]=> array(1) { [0]=> int(1) } ["prices"]=> array(1) { [0]=> float(1070) } ["enable_variations"]=> bool(false) ["variations"]=> array(0) { } } } [4]=> array(2) { ["season"]=> string(3) "256" ["price"]=> array(4) { ["periods"]=> array(1) { [0]=> int(1) } ["prices"]=> array(1) { [0]=> float(1070) } ["enable_variations"]=> bool(false) ["variations"]=> array(0) { } } } [5]=> array(2) { ["season"]=> string(3) "257" ["price"]=> array(4) { ["periods"]=> array(1) { [0]=> int(1) } ["prices"]=> array(1) { [0]=> float(1570) } ["enable_variations"]=> bool(false) ["variations"]=> array(0) { } } } [6]=> array(2) { ["season"]=> string(3) "258" ["price"]=> array(4) { ["periods"]=> array(1) { [0]=> int(1) } ["prices"]=> array(1) { [0]=> float(1715) } ["enable_variations"]=> bool(false) ["variations"]=> array(0) { } } } [7]=> array(2) { ["season"]=> string(3) "259" ["price"]=> array(4) { ["periods"]=> array(1) { [0]=> int(1) } ["prices"]=> array(1) { [0]=> float(1715) } ["enable_variations"]=> bool(false) ["variations"]=> array(0) { } } } [8]=> array(2) { ["season"]=> string(3) "260" ["price"]=> array(4) { ["periods"]=> array(1) { [0]=> int(1) } ["prices"]=> array(1) { [0]=> float(1215) } ["enable_variations"]=> bool(false) ["variations"]=> array(0) { } } } [9]=> array(2) { ["season"]=> string(3) "261" ["price"]=> array(4) { ["periods"]=> array(1) { [0]=> int(1) } ["prices"]=> array(1) { [0]=> float(1070) } ["enable_variations"]=> bool(false) ["variations"]=> array(0) { } } } [10]=> array(2) { ["season"]=> string(3) "262" ["price"]=> array(4) { ["periods"]=> array(1) { [0]=> int(1) } ["prices"]=> array(1) { [0]=> float(1070) } ["enable_variations"]=> bool(false) ["variations"]=> array(0) { } } } [11]=> array(2) { ["season"]=> string(3) "263" ["price"]=> array(4) { ["periods"]=> array(1) { [0]=> int(1) } ["prices"]=> array(1) { [0]=> float(1070) } ["enable_variations"]=> bool(false) ["variations"]=> array(0) { } } } } }
    

    if add unserialize function;

    function fiyatlar_shortcode_function() {
    $post_id = 426;
    $var = get_post_meta( $post_id, 'mphb_season_prices');
    $var = unserialize ($var);   
    var_dump($var);
    }
    
    add_shortcode( 'fiyat_tablosu', 'fiyatlar_shortcode_function' );
    

    output;

    bool(false)
    Warning: unserialize() expects parameter 1 to be string, array given in /home...
    

    if i use "return" instead of "var_dump" then output is empty and gives same as above Warning message.


  2. Here you go:

    function fiyatlar_shortcode_function() {
        $post_id = get_the_ID(); //get ID of post shortcode is embedded in
        $var = get_post_meta( $post_id, 'mphb_season_prices', true ); //retrieve as single
        var_dump($var);
    }
    
    add_shortcode( 'fiyat_tablosu', 'fiyatlar_shortcode_function' );
    

    You were very close, as I said wordpress will unserialize for you when you use get_post_meta there is no need to do this yourself.

    The reason for this error

    Warning: unserialize() expects parameter 1 to be string, array given in /home

    Is because WordPress has already unserialized the data into an array.

    You most likely will want the 3 argument to be true, otherwise the unserizlized data will be wrapped in an extra array. For example:

     //serialized data (something like this)
     a:1:{s:3:"foo";s:3:"bar";}
    
     //Original data for example
     ["foo" => "bar"]
    
     //3rd arg true
     ["foo" => "bar"]
    
     //default OR 3rd arg false -- extra array
     [["foo" => "bar"]]
    

    Or something like that.

    Oh and don’t forget to add the return $var; back in … (0.o)/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search