Wordpress多个帖子列表过滤器

时间:2014-11-19 19:28:27

标签: php wordpress filter

我的帖子列表中有多个过滤器。我让过滤器的工作原理如下:

    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'];
        }
}

现在,我的2个过滤器(位置和位置)正在运行但是单独使用。如果我尝试按位置和位置制作过滤器,它只会过滤它看起来像是最后一个查询(位置)。如何让我的查询考虑每个过滤器?

2 个答案:

答案 0 :(得分:0)

$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

答案 1 :(得分:0)

我已经这样做了:

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);
  }
}