简体   繁体   中英

Wordpress multiple post list filters

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?

$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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM