Support » Plugins » Hacks » querying and displaying post meta data multidementional array

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz


    You’ve presented the array as serialized data. When such data is taken from a DB table by get_meta(), it should be unserialized automatically. Serialized data is tricky for us mere humans to grasp. It will be easier to make sense of the data if you print_r() or var_dump() the unserialized array inside HTML <pre> tags. That view should make it more apparent how to access the individual elements.

    I currently don’t have access to my server so I’ll have to try to decipher the serialized form. I think I have it right, but I could be wrong. You have a hybrid 7×2 array. The first dimension is an indexed array, and each indexed element is an associative array. There’s a number of ways to loop through array elements for processing. Without getting into those options, you could output the hours of a single day with something like this:
    echo $key_1_value[0]['days']." ".$key_1_value[0]['hours']."<br>\n";

    Unless I’m mistaken, this should yield visible output. See if you can come up with a way to loop through the array to output all days as necessary, without hardcoding the index value (i.e. the zero needs to be a variable). If you get stuck, show us your best effort and someone should be able to set you straight.

    I’m obviously trying to get you to learn for yourself. If you can’t be bothered, let me know and I’ll show you the loop, but I truly believe you will be better off when someone doesn’t just give you all the answers.

    The example code you’re using from the get_post_meta page should be used inside a loop (foreach, while, for etc are typically used for this); because you’re pulling an array out with get_post_meta, if you try and echo that without a loop all you would typically see is ‘Array’.

    Try something like:

    $hours = get_post_meta( get_the_ID(), '_wppl_days_hours', true);
    foreach( $hours as $opening ) {
        echo $opening['days'] .': '. $opening['hours'] .'<br>';

    This should pull your multidimensional array into $hours. We can then loop over $hours, assigning each inner array to $opening where we can then access the day and hours by their named key.


    It took me a while to get the syntax, but once I found out there was a function to unserialize the variable it was much easier.

    global $wpdb;
    //Define the WordPress post meta table
    $metatable = $wpdb->prefix."postmeta";
    //Retrieve post_id of every business page
    //Formulate the SQL query
    //Using prepared statements is the correct method
    //You should be using this for security reasons
    //Use $wpdb->get_var for retrieving single result
    $business_hours = $wpdb->get_var($wpdb->prepare("SELECT meta_value FROM $metatable where post_id=%d AND meta_key='_wppl_days_hours'",$post_id_business));
    //Output the hours to your theme
    //Simple echo the variable directly containing the result query
    //echo $business_hours;
    $a = unserialize($business_hours);
    echo "<br>";
    for ($row = 0; $row < 7; $row++)
     echo $a[$row][days].": ".$a[$row][hours]."<br>";

    So in a nutshell it works:

    Sunday: 11am-10pm
    Monday: Closed
    Tuesday: 11am-12am
    Wednseday: 11am-12am
    Thursday: 11am-12am
    Friday: 11am-2am
    Saturday: 11am-2am

    get_post_meta unserialises the data for you – you don’t need to directly query the database (and you’re misusing wpdb->prepare)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘querying and displaying post meta data multidementional array’ is closed to new replies.