0

I am having this code in filter backend.Three filters are there. Each are working separate good. but when i run all filter same time its returning "OR" result. I need to make all three filters "AND"

add_filter( 'parse_query', 'wpse45436_posts_filter' );
function wpse45436_posts_filter( $query ){
    global $pagenow;
    $type = 'lessons';

    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }
    if ( 'lessons' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['age_band']) && $_GET['age_band'] != '') {
       // die("here");
        $query->query_vars['meta_key'] = 'dt_age_group';
        $query->query_vars['meta_value'] = $_GET['age_band'];
    }
}
add_filter( 'parse_query', 'wpse45437_posts_filter' );
function wpse45437_posts_filter( $query ){
    global $pagenow;
    $type = 'lessons';
    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }
    if ( 'lessons' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['module_no']) && $_GET['module_no'] != '') {
        $ar = array("A"=>"4-5","B"=>"6-7","C"=>"8-9","D"=>"10-11","E"=>"12-13","F"=>"14-15");
        $ar = array_flip($ar);
        $chr = array("A","B","C","D","E","F");
        $mod_no = $_GET['module_no'];
        if ( 'lessons' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['age_band']) && $_GET['age_band'] != '') {
       // die("here");
       $mod_no = $mod_no.$ar[$_GET['age_band']];
    }
        $query->query_vars['meta_key'] = 'dt_module_no';
        $query->query_vars['meta_value'] = $mod_no;
         $query->query_vars['meta_compare'] = 'LIKE';
    }
}
add_filter( 'parse_query', 'wpse45438_posts_filter' );
function wpse45438_posts_filter( $query ){
    global $pagenow;
    $type = 'lessons';

    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }
    if ( 'lessons' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['lesson_no']) && $_GET['lesson_no'] != '') {
       // die("here");
        $query->query_vars['meta_key'] = 'dt_lesson_no';
        $query->query_vars['meta_value'] = $_GET['lesson_no'];
        $query->query_vars['meta_relation'] = 'AND';
    }
}
1

Since you are querying for multiple meta_key's, you will want to put your meta_key queries in an array. A good example is this posting on StackExchange.

For each of your functions you'll want to replace the last three lines where you set the ['meta_key'], ['meta_value'], and ['meta_relation'] with something like this:

if( ! isset($query->query_vars['meta_query']) ) {
   $query->query_vars['meta_query'] = array();
   }
// setup this functions meta values
$meta = array (
   'key'  =>   'dt_module_no',
   'value' =>   $mod_no,
   'compare' => 'LIKE'
 );
 // append to meta_query array
 $query->query_vars['meta_query'][] = $meta;

By default each array in the meta_query will be 'AND'ed in the query.

This code is untested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.