My goal is to only show posts with opening hours after business hours, for a given day. So, e.g. I have a (custom) post with following meta_key and meta_value:

meta_key = 'monday, meta_value = '14:00 - 22:00'

The business hours are fixed: '08:00 - 18:00'

I'd like to add a filter to my custom wp_query. Only, I am getting stuck at defining a function.

My logic input (definitely not correct & complete) for the function:

$from_std = strtotime("08:00");
$to_std = strtotime("18:00");                   

$open = $my_meta['monday'];
list($from, $hyphen, $to) = explode(' ', $open);
$from_bus = strtotime($from); 
$to_bus = strtotime($to);

if ($from_bus < $from_std) { $show = 1; } // before standard starting time - so SHOW
  else {if ($to_bus > $to_std) { $show = 1; }} // after standard closing time - so SHOW

if (empty($from_bus)) { show = 0; } // $var is either 0, empty, or not set at all - so do NOT show

Help on a function is very much appreciated.

  • strtotime is not that helpful for what you're trying to do. compare php.net/manual/en/function.strtotime.php . instead you can just do a string compare because '00:00' < '00:30' < '01:00' and so on. just ensure those values are following the same format.
    – hakre
    Commented Dec 16, 2010 at 15:22
  • OK, that would be more efficient then. Thanks for pointing that out. Could you give me directions with translating this into a function with a where-filter?
    – Henk Jan
    Commented Dec 17, 2010 at 9:33
  • What is a where-filter? You want to convert that into a SQL query?
    – hakre
    Commented Feb 7, 2011 at 23:30

1 Answer 1


You can use the WP query_posts to filter about your custom values; ean example:

if (have_posts()) :
while ( have_posts() ) : the_post();

But, i think you must replace the time-values for readable string, maybe woth strotime, that you have the same format for the query.

  • thanks - that's how it worked out for me. However in the end I set up things differently. The main problem was that opening hours were recorded passing a day limit. So e.g. open from 22:00 - 05.00. I tried some nifty logic on this - but the results were never 100% correct. Now, I leave it up to human interpretation if a business is open after office hours by indicating if that is not the case. Now the results are 100% correct - leaving human errors out.
    – Henk Jan
    Commented Mar 22, 2011 at 7:24
  • @Henk jan: maybe you can add your solution here and we see on the code of the timestamp. Its usefull for all readers and maybe you become an better solution.
    – bueltge
    Commented Mar 23, 2011 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.