开发者

WordPress: Using a Where Clause With A Custom Field

开发者 https://www.devze.com 2023-02-15 20:01 出处:网络
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 cus

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 fil开发者_C百科ter itself is bad. Any help or guidance would be greatly appreciated. Thanks!


If your code is as in your question you might want to try putting your add_filter call before the query_posts one.

If you are using Wordpress 3.1 you should be able to use the new meta_query parameter, something like this:

query_posts(
  array(
    'cat' => '4',
    'meta_key' => 'TheDate',
    'meta_query' => array(
      array(
        'key' => 'TheDate',
        'value' => date('Ymd'),
        'compare' => '>=',
        'type' => 'NUMERIC' 
      )
    ),
    'order' => 'ASC',
    'orderby' => 'meta_value_num',  
    'posts_per_page' => -1,
  )
);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号