• anooprai790

    (@anooprai790)


    if($asCityLength>0 && $asProfessionLength>0){

    echo “Hello”;
    $args[‘meta_query’] = array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’ => ‘user_registration_radio_user_type’,
    ‘value’ => ‘Lawyer’,
    ‘compare’ => ‘LIKE’,
    ‘type’ => ‘CHAR’,
    ),
    array(
    ‘key’ => ‘user_registration_city’,
    ‘value’ => ‘Delhi’,
    ‘compare’ => ‘LIKE’,
    ‘type’ => ‘CHAR’,
    )
    );

    return $args;
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter anooprai790

    (@anooprai790)

    Search on individual fields is working fine , but when I start searching on multiple fields , its stop working .

    Individual searching that is working now is …

    if ( ($asProfessionLength>0)){

    $args[‘meta_key’] = ‘user_registration_radio_user_type’;
    $args[‘meta_value’] = $asProfession;
    $args[‘meta_compare’] = ‘LIKE’;
    if( isset( $args[‘asProfession’] ) ) unset($args[‘asProfession’]);
    return $args;
    }

    Plugin Author HelgaTheViking

    (@helgatheviking)

    It’s not clear to me how/where you are adding this code… filtering sul_user_query_args ? But most of all, I’m not able to dig to deep into custom queries. My recommendation would be to make sure that you can run a regular new WP_User_Query with the results you want to validate that your query is working. And then port that back to this plugin.

    Thread Starter anooprai790

    (@anooprai790)

    Hello ,

    I am using above code into function.php file.

    When I am filtering this for one custom fields , its working fine.

    This is the code that working okay …

    if ( ($asProfessionLength>0)){

    $args[‘meta_key’] = ‘user_registration_radio_user_type’;
    $args[‘meta_value’] = $asProfession;
    $args[‘meta_compare’] = ‘LIKE’;
    if( isset( $args[‘asProfession’] ) ) unset($args[‘asProfession’]);
    return $args;
    }

    ======> Now My concern is this when I am using filter on two custom fields using AND login , why its not giving any result.

    ….. This code is returning no data

    I am using this code in same function in behalf of length of input fields data .

    Please let me know whats wrong in this logic .

    if($asCityLength>0 && $asProfessionLength>0){
    $args[‘meta_query’] = array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’ => ‘user_registration_radio_user_type’,
    ‘value’ => ‘Lawyer’,
    ‘compare’ => ‘LIKE’,
    ‘type’ => ‘CHAR’,
    ),
    array(
    ‘key’ => ‘user_registration_city’,
    ‘value’ => ‘Delhi’,
    ‘compare’ => ‘LIKE’,
    ‘type’ => ‘CHAR’,
    )
    );

    return $args;
    }

    ========>

    This is the whole function , Please check where I am doing wrong…

    function kia_meta_search( $args ){

    // This $_GET is the name field of the custom input in search-author.php.
    $search = ( isset($_GET[‘as’]) ) ? sanitize_text_field($_GET[‘as’]) : false ;
    $asProfession = ( isset($_GET[‘asProfession’]) ) ? sanitize_text_field($_GET[‘asProfession’]) : false ;
    $asCity = ( isset($_GET[‘asCity’]) ) ? sanitize_text_field($_GET[‘asCity’]) : false ;

    $searchLength = strlen($search);
    $asProfessionLength = strlen($asProfession);
    $asCityLength = strlen($asCity);

    if ( ($searchLength >0)){

    $args[‘meta_key’] = ‘first_name’;
    $args[‘meta_value’] = $search;
    $args[‘meta_compare’] = ‘LIKE’;
    if( isset( $args[‘search’] ) ) unset($args[‘search’]);
    return $args;
    }

    if($asCityLength>0 && $asProfessionLength>0){

    echo “Hello”;
    $args[‘meta_query’] = array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’ => ‘user_registration_radio_user_type’,
    ‘value’ => ‘Lawyer’,
    ‘compare’ => ‘LIKE’,
    ‘type’ => ‘CHAR’,
    ),
    array(
    ‘key’ => ‘user_registration_city’,
    ‘value’ => ‘Delhi’,
    ‘compare’ => ‘LIKE’,
    ‘type’ => ‘CHAR’,
    )
    );

    return $args;
    } else {

    if ( ($asProfessionLength>0)){

    $args[‘meta_key’] = ‘user_registration_radio_user_type’;
    $args[‘meta_value’] = $asProfession;
    $args[‘meta_compare’] = ‘LIKE’;
    if( isset( $args[‘asProfession’] ) ) unset($args[‘asProfession’]);
    return $args;
    }

    if ( ($asCityLength>0)){

    $args[‘meta_key’] = ‘user_registration_city’;
    $args[‘meta_value’] = $asCity;
    $args[‘meta_compare’] = ‘LIKE’;
    if( isset( $args[‘asCity’] ) ) unset($args[‘asCity’]);
    return $args;
    }
    }

    if($asCityLength ==0 && $asProfessionLength==0 && $searchLength==0){
    $args[‘meta_key’] = ”;
    $args[‘meta_value’] = ”;
    $args[‘meta_compare’] = ”;

    return $args;

    }

    }
    add_filter(‘sul_user_query_args’, ‘kia_meta_search’);

    Please help , I stuck here from so many days.

    Plugin Author HelgaTheViking

    (@helgatheviking)

    Sorry, I don’t really have the availability to take on custom gigs. I would take a look at the Custom Query ID parameter mentioned here:
    https://www.ads-software.com/plugins/simple-user-listing/#how%20to%20create%20very%20complex%20user%20queries%20%7C%20how%20to%20query%20multiple%20meta%20keys

    Which is also my best example of querying multiple meta fields. It’s hard to parse your code since you didn’t format it, but it 1. looks like you may be return $args back early…. always use opening and closing parens {} And you may also need to always unset the $args["search"]. The $_GET['as'] input is set by Simple User Listings to be the search… and search is looking in the user’s name or maybe display name, I can’t remember.

    If looks like your code is trying to map the $_GET['as'] field to the first name meta? So if you leave search in the $args and try to do a meta query by other params you may never get matching results.

    Good luck.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Multiple Search is not working’ is closed to new replies.