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
code I'm currently using;
and output;
if add unserialize function;
output;
if i use "return" instead of "var_dump" then output is empty and gives same as above Warning message.
Here you go:
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
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:
Or something like that.
Oh and don’t forget to add the
return $var;
back in … (0.o)/