Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter bdev9

    (@bdev9)

    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
    $post_id_business=$post->ID;
    
    //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);
    //print_r($a);
    
    echo "<br>";
    for ($row = 0; $row < 7; $row++)
    {
     echo $a[$row][days].": ".$a[$row][hours]."<br>";
    }
    //var_dump($a);

    So in a nutshell it works:

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

Viewing 1 replies (of 1 total)