I currently created a custom field for a custom post type that stores a date in the format dd/mm/yyyy is there a way to query the posts by the month?

I "kind of" got the functionality I need using the following query:


The meta_value parameter would be the month but since I'm using LIKE to compare if I have a date like 10/04/2011 it'd be returned even though it's not from october.

I know there's a RLIKE expression in MySQL that lets you use regex for the LIKE condition but I'm not sure if wordpress lets you use it in any way.

Thanks in advance!

1 Answer 1


It is not clear what you're using as a comparison, but WP Query supports meta query comparisons and even has a DATE type.

For example:

    $query = new WP_Query( array( 
                       'post_type' => 'attraction',
                       'meta_key' => 'attraction_date', 
                       'meta_value' => '10', 
                       'meta_compare' => 'LIKE', 
                       'type' => 'DATE' ) );

Using 10 is not going to get you very far, you should use a timestamp.

I have not tested the date parameter in WP QUERY, it should be using/converting all dates to a unix timestamp using strtotime, if not I would suggest doing so.


  • I see, so in theory do you think if I use something like: meta_value = strtotime('october') and keep the rest the same it should work? I can't test right now but I'll make sure to give it a try once I get home Sep 16, 2011 at 2:52
  • In theory yes, but I would check out the default DATE first.
    – Wyck
    Sep 16, 2011 at 2:57
  • Sadly it didn't work but I had an idea, since I need to query by the month and the custom field format is dd/mm/yyyy what I did was 'meta_value' => '/10/' this way it wouldn't return posts with 10 as the day or if the year was 2010 Sep 16, 2011 at 3:09
  • You should use a variable for 'meta_value' => $my_date, like $my_date = strotime($whatever)
    – Wyck
    Sep 16, 2011 at 5:22
  • I did I'm using the 10 as an example but I'm getting the value as a get parameter like $my_date = strtotime($_GET['month']) where the month equals to january, february or any other but it didn't work event when I set the type as DATE. Sep 16, 2011 at 16:53

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.