WordPress: Using a Where Clause With A Custom Field

Posted by Steve Wilkison on Stack Overflow See other posts from Stack Overflow or by Steve Wilkison
Published on 2011-03-07T16:08:32Z Indexed on 2011/03/07 16:10 UTC
Read the original article Hit count: 239

Filed under:
|

I have a bunch of events that are listed on a particular page. Each event is a post. I need them to display in the order in which they occur, NOT the order of the posting date. So, I've created a custom field called TheDate and enter in the date in this format for each one: 20110306.

Then, I wrote my query like this:

query_posts( array ( 'cat' => '4', 'posts_per_page' => -1, 'orderby' => 'meta_value_num',      
'meta_key' => 'TheDate', 'order' => 'ASC' ) );

Works perfectly and displays the events in the correct order. However, I also want it to ONLY display dates from today onward. I don't want it to display dates which have passed. It seems the way to do this is with a "filter." I tried this, but it doesn't work.

$todaysdate = date('Ymd');
query_posts( array ( 'cat' => '4', 'posts_per_page' => -1, 'orderby' => 'meta_value_num',  
'meta_key' => 'TheDate', 'order' => 'ASC' ) );
function filter_where( $where = '' ) {
$where .= "meta_value_num >= $todaysdate";
return $where;
}
add_filter( 'posts_where', 'filter_where' );

I figure it's just a matter of where I'm using this filter, I probably have it in the wrong place. Or maybe the filter itself is bad. Any help or guidance would be greatly appreciated. Thanks!

© Stack Overflow or respective owner

Related posts about Wordpress

Related posts about where-clause