[wp-hackers] Get posts by time range

Dino Termini dino at duechiacchiere.it
Mon Aug 13 20:00:15 UTC 2012


Thank you guys. Hopefully the new wp query extension will be added to 3.5 before it ships. In the meanwhile I think the custom field workaround is a reasonable compromise ;)

Cheers
Dino

On Aug 13, 2012, at 3:47 PM, John Blackbourn <johnbillion+wp at gmail.com> wrote:

> On 13 August 2012 20:31, Dino Termini <dino at duechiacchiere.it> wrote:
>> Hi all,
>> 
>> I have a question about query_posts. I need to retrieve all the articles having a given custom field associated to them, but limited to a specific time range. Is there any way of achieving this without adding a filter to posts_where?
> 
> As Bill points out, there's not a way to do this in core yet. I've
> overcome this previously by adding an 'updated' custom field to every
> post with a MySQL-formatted date string whenever the post is updated
> (or created, depending on the use-case). You can then use that in a
> meta_query like this:
> 
> $meta_query = array(
>    array(
>        'key'     => 'my_key',
>        'value'   => 'my_value'
>    ),
>    array(
>        'key'     => 'updated',
>        'value'   => date( 'Y-m-d H:i:s' ),
>        'compare' => '>=',
>        'type'    => 'DATETIME'
>    )
> ) );
> 
> The 'value' argument for the date can of course be something else such
> as date(  'Y-m-d H:i:s', strtotime( '-3 days' ).
> 
> HTH
> John
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list