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,
  )
);
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论