Skip Navigation

[Resolved] I have added a number of filters for wpv_filter_user_query but only the last one

This support ticket is created 7 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 7 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#577578

I am trying to:
create user filter for different view using wpv_filter_user_query.
I have created filters for 3 different views. each filter checks to make sure it is only working on the right view and I have tested the code and it works fine.

However when I have all three filter in the one functions.php file only the filter that relates to the last function is applied to the view even though the return query string is created and return in all cases, the filter only gets applied when the view is related to the last function filter.

Link to a page where the issue can be seen:

I expected to see:

Instead, I got:

#577645

Hi, please share the code you have used in your functions.php file so I can review the filters and functions. Thanks!

#577698
add_filter( 'wpv_filter_user_query', 'host_family_search_engine' , 10, 3);


function host_family_search_engine( $query_args, $view_id, $setting ) {
global $WP_Views;
//if(isset($_GET['county-search'])){
//    debug_to_console('County Search ' . $_GET['county-search']); 
//}
 //   foreach($_GET as $key=>$post_data){
 //       debug_to_console('You posted:' . $key . ' = ' . $post_data );
 //   }

    
if ( $WP_Views->current_view == 2704 || $WP_Views->current_view == 2704) {
    $kids_search = 0;
    global $wpdb;
        $attributes = $WP_Views->view_shortcode_attributes;
        if(isset($attributes[0][$attr])){
            $res = $attributes[0][$attr];
        }

    if ($_GET["kids-gender"] == "boys" ){
        $gender = "'1'";

    }elseif($_GET["kids-gender"] == "girls"){
        $gender = "'2'";

    }elseif($_GET["kids-gender"] == "either"){
        $gender = "'1', '2'";

    }
$ls = array(1, 2, 3);


if (in_array($_GET["host-family-kids-ages"], $ls)){
    //debug_to_console('Update check 3 ' . $_GET["host-family-kids-ages"]);
    $kids_search = 1;
    if ($_GET["host-family-kids-ages"] == 2){
        $time = new DateTime('now');
        $newtime = $time->modify('-10 year')->format('Y-m-d');
        $date1 = strtotime($newtime);
        $time = new DateTime('now');
        $newtime = $time->modify('-16 year')->format('Y-m-d');
        $date2 = strtotime($newtime);
    }elseif($_GET["host-family-kids-ages"] == 1){
        $time = new DateTime('now');
        $newtime = $time->modify('-15 year')->format('Y-m-d');
        $date1 = strtotime($newtime);
        $time = new DateTime('now');
        $newtime = $time->modify('-30 year')->format('Y-m-d');
        $date2 = strtotime($newtime);
    }
    elseif($_GET["host-family-kids-ages"] == 3){
        $time = new DateTime('now');
        $newtime = $time->modify('-5 year')->format('Y-m-d');
        $date1 = strtotime($newtime);
        $time = new DateTime('now');
        $newtime = $time->modify('-11 year')->format('Y-m-d');
        $date2 = strtotime($newtime);        
    } 
    debug_to_console('Update check 111' . $query . " ======");


//if (in_array($_GET["host-family-kids-ages"], $ls)){
$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-1-gender' AND mt1.meta_value IN (". $gender .") ) OR 
(mt1.meta_key = 'wpcf-child-1-dob' AND mt1.meta_value BETWEEN " . $date2 . " AND " . $date1 . ") 
GROUP BY mt1.user_id 
HAVING COUNT(mt1.user_id) > 1");

    $values1 = $wpdb->get_col( $query );
    //print_r($query);    


$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-2-gender' AND mt1.meta_value IN (". $gender .") ) OR 
(mt1.meta_key = 'wpcf-child-2-dob' AND mt1.meta_value BETWEEN " . $date2 . " AND " . $date1 . ") 
GROUP BY mt1.user_id 
HAVING COUNT(mt1.user_id) > 1");

    $values2 = $wpdb->get_col( $query );


$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-3-gender' AND mt1.meta_value IN (". $gender .") ) OR 
(mt1.meta_key = 'wpcf-child-3-dob' AND mt1.meta_value BETWEEN " . $date2 . " AND " . $date1 . ") 
GROUP BY mt1.user_id 
HAVING COUNT(mt1.user_id) > 1");

    $values3 = $wpdb->get_col( $query );


$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-4-gender' AND mt1.meta_value IN (". $gender .") ) OR 
(mt1.meta_key = 'wpcf-child-4-dob' AND mt1.meta_value BETWEEN " . $date2 . " AND " . $date1 . ") 
GROUP BY mt1.user_id 
HAVING COUNT(mt1.user_id) > 1");

    $values4 = $wpdb->get_col( $query );
    
    
$values_full = array_merge($values1, $values2, $values3, $values4);

}elseif($_GET["host-family-kids-ages"] == 4 ){

    //debug_to_console('Update check 222 ' . $gender . " ======");
$kids_search =1;
$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where (mt1.meta_key = 'wpcf-child-1-gender' AND mt1.meta_value IN (". $gender .") )");
//print_r($query);
    $values1 = $wpdb->get_col( $query );
    


$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-2-gender' AND mt1.meta_value IN (". $gender .") )");

    $values2 = $wpdb->get_col( $query );


$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-3-gender' AND mt1.meta_value IN (". $gender .") )");

    $values3 = $wpdb->get_col( $query );


$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-4-gender' AND mt1.meta_value IN (". $gender .") )");

    $values4 = $wpdb->get_col( $query );
    
$values_full = array_merge($values1, $values2, $values3, $values4);

}
print_r($values_full);
//}elseif($_GET["host-family-kids-ages"] == 4 ){ 


//}
    debug_to_console('Update check 988 ' . $kids_search . " ======");
    
    if (!empty($_GET["county-search-dropdown"] && $kids_search === 1)) {
    $query_args = array(
        'include' => $values_full ,
        'meta_query' => array(
  		    'key'     => 'wpcf-county',
  		    'value'   => $_GET['county-search-dropdown'],//'9',
  		    'compare' => '='
    ));
    //return $query_args;
    print_r($query_args);
    return $query_args;
    }elseif(!empty($_GET["county-search-dropdown"] && $kids_search === 0)){
    $query_args = array(
        'meta_query' => array(
  		    'key'     => 'wpcf-county',
  		    'value'   => $_GET['county-search-dropdown'],//'9',
  		    'compare' => '='
    ));
    
    print_r("++++++++++++ 2 ++++++++++++");
    return $query_args;
    }elseif(empty($_GET["county-search-dropdown"] && $kids_search === 1)){  

        $query_args = array('include' => $values_full);
        //
    print_r("++++++++++++ 3 ++++++++++++");
    return $query_args;
    }
    
  //$query_args = array( 'role' => 'Administrator' );

   //print_r(" - 123 Start");
   //print_r($search_args);
   //print_r(" - 123 Finish   =======================   ");
    //debug_to_console('test ' . $WP_Views->current_view);
    //print_r("start - ");
    //print_r($query_args);
    //print_r(" - Finish");
    //return $query_args;

    }else{
        
       debug_to_console('host_family_search_engine ' . $WP_Views->current_view); 
    } 
    
}



add_filter( 'wpv_filter_user_query', 'host_family_search_engine_shared' , 10, 3);


function host_family_search_engine_shared( $query_args, $view_id, $setting ) {
global $WP_Views;

    
if ( $WP_Views->current_view == 1984 || $WP_Views->current_view == 1984) {
    $kids_search = 0;
    global $wpdb;    
        $attributes = $WP_Views->view_shortcode_attributes;
        if(isset($attributes[0][$attr])){
            $res = $attributes[0][$attr];
        }

    if ($_GET["kids-gender"] == "boys" ){
        $gender = "'1'";

    }elseif($_GET["kids-gender"] == "girls"){
        $gender = "'2'";

    }elseif($_GET["kids-gender"] == "either"){
        $gender = "'1', '2'";

    }
$ls = array(1, 2, 3);


if (in_array($_GET["host-family-kids-ages"], $ls)){
    //debug_to_console('Update check 3 ' . $_GET["host-family-kids-ages"]);
    $kids_search = 1;
    if ($_GET["host-family-kids-ages"] == 2){
        $time = new DateTime('now');
        $newtime = $time->modify('-10 year')->format('Y-m-d');
        $date1 = strtotime($newtime);
        $time = new DateTime('now');
        $newtime = $time->modify('-16 year')->format('Y-m-d');
        $date2 = strtotime($newtime);
    }elseif($_GET["host-family-kids-ages"] == 1){
        $time = new DateTime('now');
        $newtime = $time->modify('-15 year')->format('Y-m-d');
        $date1 = strtotime($newtime);
        $time = new DateTime('now');
        $newtime = $time->modify('-30 year')->format('Y-m-d');
        $date2 = strtotime($newtime);
    }
    elseif($_GET["host-family-kids-ages"] == 3){
        $time = new DateTime('now');
        $newtime = $time->modify('-5 year')->format('Y-m-d');
        $date1 = strtotime($newtime);
        $time = new DateTime('now');
        $newtime = $time->modify('-11 year')->format('Y-m-d');
        $date2 = strtotime($newtime);        
    } 
    debug_to_console('Update check 111' . $query . " ======");


//if (in_array($_GET["host-family-kids-ages"], $ls)){
$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-1-gender' AND mt1.meta_value IN (". $gender .") ) OR 
(mt1.meta_key = 'wpcf-child-1-dob' AND mt1.meta_value BETWEEN " . $date2 . " AND " . $date1 . ") 
GROUP BY mt1.user_id 
HAVING COUNT(mt1.user_id) > 1");

    $values1 = $wpdb->get_col( $query );
    //print_r($query);    


$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-2-gender' AND mt1.meta_value IN (". $gender .") ) OR 
(mt1.meta_key = 'wpcf-child-2-dob' AND mt1.meta_value BETWEEN " . $date2 . " AND " . $date1 . ") 
GROUP BY mt1.user_id 
HAVING COUNT(mt1.user_id) > 1");

    $values2 = $wpdb->get_col( $query );


$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-3-gender' AND mt1.meta_value IN (". $gender .") ) OR 
(mt1.meta_key = 'wpcf-child-3-dob' AND mt1.meta_value BETWEEN " . $date2 . " AND " . $date1 . ") 
GROUP BY mt1.user_id 
HAVING COUNT(mt1.user_id) > 1");

    $values3 = $wpdb->get_col( $query );


$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-4-gender' AND mt1.meta_value IN (". $gender .") ) OR 
(mt1.meta_key = 'wpcf-child-4-dob' AND mt1.meta_value BETWEEN " . $date2 . " AND " . $date1 . ") 
GROUP BY mt1.user_id 
HAVING COUNT(mt1.user_id) > 1");

    $values4 = $wpdb->get_col( $query );
    
    
$values_full = array_merge($values1, $values2, $values3, $values4);

}elseif($_GET["host-family-kids-ages"] == 4 ){

    //debug_to_console('Update check 222 ' . $gender . " ======");
$kids_search =1;
$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where (mt1.meta_key = 'wpcf-child-1-gender' AND mt1.meta_value IN (". $gender .") )");
//print_r($query);
    $values1 = $wpdb->get_col( $query );
    


$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-2-gender' AND mt1.meta_value IN (". $gender .") )");

    $values2 = $wpdb->get_col( $query );


$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-3-gender' AND mt1.meta_value IN (". $gender .") )");

    $values3 = $wpdb->get_col( $query );


$query = $wpdb->prepare("SELECT mt1.user_id FROM $wpdb->wpgi_usermeta wpgi_usermeta mt1 where 
(mt1.meta_key = 'wpcf-child-4-gender' AND mt1.meta_value IN (". $gender .") )");

    $values4 = $wpdb->get_col( $query );
    
$values_full = array_merge($values1, $values2, $values3, $values4);

}

    debug_to_console('Update check 988 ' . $kids_search . " ======");
    
    if (!empty($_GET["county-search-dropdown"] && $kids_search === 1)) {
    $query_args = array(
        'include' => $values_full ,
        'meta_query' => array(
  		    'key'     => 'wpcf-county',
  		    'value'   => $_GET['county-search-dropdown'],//'9',
  		    'compare' => '='
    ));
    return $query_args;
    
    }elseif(!empty($_GET["county-search-dropdown"] && $kids_search === 0)){
    $query_args = array(
        'meta_query' => array(
  		    'key'     => 'wpcf-county',
  		    'value'   => $_GET['county-search-dropdown'],//'9',
  		    'compare' => '='
    ));
    return $query_args;
    //print_r("++++++++++++ 2 ++++++++++++");
    }elseif(empty($_GET["county-search-dropdown"] && $kids_search === 1)){  

        $query_args = array('include' => $values_full);
        return $query_args;
    //print_r("++++++++++++ 3 ++++++++++++");
    }

    }elseif ( $WP_Views->current_view == 2704 || $WP_Views->current_view == 2704) {


    $kids_search = 0;
    global $wpdb;
        $attributes = $WP_Views->view_shortcode_attributes;
        if(isset($attributes[0][$attr])){
            $res = $attributes[0][$attr];
        }

    if ($_GET["kids-gender"] == "boys" ){
        $gender = "'1'";

    }elseif($_GET["kids-gender"] == "girls"){
        $gender = "'2'";

    }elseif($_GET["kids-gender"] == "either"){
        $gender = "'1', '2'";

    }
#577700

Hi, I'm only able to see two filter functions here and two View IDs. I'm afraid the forum may have cut off the code you provided. It will probably be best for me to work on a clone of your site so I can see what's happening. I can install the Duplicator plugin to make a copy and install it locally. If that's okay with you, please provide login credentials here and I will begin testing. I have enabled private reply fields.