miriamdepaula

WordPress: Query for custom fields: Day, Month and Year

Apr 27th, 2012
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.72 KB | None | 0 0
  1. <?php
  2. global $wp_query;
  3.  
  4. function filter_where($where = '') {
  5.    
  6.     if( !is_admin() ){
  7.        
  8.         if( isset($_GET['dia']) )
  9.             $where.= " AND DAY(meta_value) = '". $_GET['dia']. "'";
  10.            
  11.         if( isset($_GET['mes']) )
  12.             $where.= " AND MONTH(meta_value) = '". $_GET['mes']. "'";
  13.        
  14.         if( isset($_GET['ano']) )
  15.             $where.= " AND YEAR(meta_value) = '". $_GET['ano']. "'";
  16.        
  17.     }  
  18.     return $where;
  19. }
  20.  
  21. add_filter('posts_where', 'filter_where');
  22.  
  23. $args = array_merge( $wp_query->query, array( 'meta_key' => 'CUSTOM_FIELD_DATA', 'orderby' => 'meta_value', 'order' => 'desc' ) );
  24. query_posts( $args );
  25.  
  26. //CUSTOM_FIELD_DATA: é o nome do seu custom field que contém a data. Lembrando que a data deve estar no formato YYYY-MM-DD
  27. ?>
Advertisement
Add Comment
Please, Sign In to add comment