trevorpao
6/13/2014 - 2:39 AM

pre_get_posts with meta_query from::http://wordpress.stackexchange.com/questions/20237/using-meta-query-how-can-i-filter-by-a-custom-field-

add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
  if ( is_home() ) {
    $query->set( 'post_type', 'event' );
    $query->set( 'meta_key', '_start_date' );
    $query->set( 'orderby', 'meta_value_num' );
    $query->set( 'order', 'ASC' );
    $query->set( 'meta_query', array(
        array(
              'key' => '_start_date'
        ),
        array(
              'key' => '_end_date',
              'value' => time(),
              'compare' => '>=',
              'type' => 'numeric'
        )
    ));
  }
  return $query;
}

// part2::change post type in search result
function search_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set('post_type', array( 'post', 'movie' ) );
    }
  }
}

add_action('pre_get_posts','search_filter');