Wordpress multiple post list filters

2019-09-06 02:25发布

I have multiple filters in my posts list. i make the filters work like this :

    add_filter( 'parse_query','event_table_filter' );
    function event_table_filter( $query ){

        $type = 'event';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }

        if ( 'event' == $type && is_admin() && isset($_GET['ADMIN_FILTER_FIELD_LOCATION']) && $_GET['ADMIN_FILTER_FIELD_LOCATION'] != '') {
            $query->query_vars['meta_key'] = 'location';
            $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_LOCATION'];
        }

        if ( 'event' == $type && is_admin() && isset($_GET['ADMIN_FILTER_FIELD_FEATURED']) && $_GET['ADMIN_FILTER_FIELD_FEATURED'] != '') {
            $query->query_vars['meta_key'] = 'position';
            $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_FEATURED'];
        }
}

right now, my 2 filters (location and position) are working but individually. If i try to make a filters by position AND Location, it filtering only by what it seem to be the last query (position). how can i make my query take in consideration every filters?

2条回答
贼婆χ
2楼-- · 2019-09-06 03:07
$args = array(
    'post_type' => 'posttypehere',
    'meta_query' => array(
       'relation' => 'OR',
        array(
              'key' => '_price',
              'value' => array($_POST['p_from'], $_POST['p_to']),
              'type' => 'CHAR',
              'compare' => 'BETWEEN'
              ), 
         array(
            'key' => 'somekey',
            'value' => array($_POST['p_from'], $_POST['p_to']),
            'type' => 'CHAR',
            'compare' => 'BETWEEN'
             ),
         array(
            'key' => 'anotherkey',
            'value' => array($_POST['p_from'], $_POST['p_to']),
            'type' => 'CHAR',
            'compare' => 'BETWEEN'
        ),

    )
);

$query = new WP_Query( $args );
if ( $query->have_posts() ) :
    while ($query->have_posts()) : $query->the_post();
             echo $post_id = get_the_ID();
    endwhile;
endif;

    enter code here
查看更多
冷血范
3楼-- · 2019-09-06 03:07

I have done like this:

function  wpr_manager_filter($query) {
  global $pagenow;
  global $typenow;

  $current_page = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';

  if ( is_admin()
    && 'properties' == $typenow
    && 'edit.php' == $pagenow
  )
  {
    $queryParamsCounter = 0;
    if (isset( $_GET['city-filter'] ) && $_GET['city-filter'] != '-1')
    {
      $cityId = (int)$_GET['city-filter'];
      $queryParamsCounter++;
    }
    if (isset( $_GET['visibility-filter'] ) && $_GET['visibility-filter'] != '-1')
    {
      $queryParamsCounter++;
      $visibility = (int)$_GET['visibility-filter'];
    }

    $meta_query = array();

    if ($queryParamsCounter > 1) {
      $meta_query['relation'] = 'AND';
    }

    if (isset($cityId)) {
      $meta_query[] =       array(
        'key'     => 'city',
        'value'   => $cityId,
        'compare' => '=',
        'type'    => 'NUMERIC',
      );
    }
    if (isset($visibility)) {
      $meta_query[] = array(
        'key'     => 'visibility',
        'value'   => $visibility,
        'compare' => '=',
        'type'    => 'NUMERIC',
      );
    }

    $query->set( 'meta_query', $meta_query);
  }
}

查看更多
登录 后发表回答