Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

query_posts('post_type=attraction&meta_key=attraction_date&meta_value=10&meta_compare=LIKE');

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!

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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.

http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
http://php.net/manual/en/function.strtotime.php

share|improve this answer
    
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 –  javiervd Sep 16 '11 at 2:52
    
In theory yes, but I would check out the default DATE first. –  Wyck Sep 16 '11 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 –  javiervd Sep 16 '11 at 3:09
    
You should use a variable for 'meta_value' => $my_date, like $my_date = strotime($whatever) –  Wyck Sep 16 '11 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. –  javiervd Sep 16 '11 at 16:53
add comment

Your Answer

 
discard

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.