Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
strtotime is not that helpful for what you're trying to do. compare . 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 Dec 16 '10 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 Dec 17 '10 at 9:33
What is a where-filter? You want to convert that into a SQL query? – hakre Feb 7 '11 at 23:30

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
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 Mar 22 '11 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 Mar 23 '11 at 13:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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