daily pastebin goal
34%
SHARE
TWEET

WP Query vs. Pre_Get_Posts

a guest Feb 17th, 2013 234 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  /* Modify the Query Directly */
  2. $today = date( 'Y-m-d' );
  3.  
  4. $args = array(
  5.    'post_type' => 'bgmp',
  6.    'meta_query' => array(
  7.      array(
  8.          'key' => 'coupon_end_date',
  9.          'value' => $today,
  10.          'compare' => '>=',
  11.          'type' => 'DATETIME'
  12.      )
  13.  )
  14.  );
  15. $the_query = new WP_Query( $args );
  16.  
  17. /* OR use pre_get_posts */
  18.  
  19. add_action( 'pre_get_posts', 'fm_modify_coupon_query' );
  20. function fm_modify_coupon_query( $query ) {
  21.  
  22.         // Check to make sure we're on the front end, is main query, and within the coupon taxonomy
  23.         if( ! is_admin() && $query->is_main_query() && is_tax('coupon') ) {
  24.    
  25.     $today = date( 'Y-m-d' );
  26.                 $query->set('meta_key', 'coupon_end_date');
  27.                 $query->set('meta_value', $today);
  28.                 $query->set('compare', '>=');
  29.                 $query->set('type', 'DATETIME');
  30.         }
  31.  
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top