Support

Account

Home Forums General Issues Filter by user meta with checkbox not working

Unread

Filter by user meta with checkbox not working

  • I am working on a custom search filter which is filter users by user meta. All the user field were create by Advance Custom field.

    I am trying to filter users by checkbox values which user select from there profile. but some how data is not filtering. I even tried custom php queries to pull the data by checkbox values but not working. I think the problem is in the checkbox value format saving into the database.

    a:7:{i:0;s:17:”business_coaching”;i:1;s:10:”depression”;i:2;s:8:”finances”;i:3;s:9:”mentoring”;i:4;s:10:”retirement”;i:5;s:17:”work_life_balance”;i:6;s:15:”career_coaching”;}

    And below is my code to filter the data by ajax.

    Search Form

    <form name=”” action=”<?php site_url() ?>” method=”get”>
    <input type=”hidden” name=”s” value=”find_coach”>
    <div class=”searchSection searchSection_services”>
    <div class=”sectionContent filter_list”>
    <?php
    acf_form([
    ‘field_groups’ => [’50’],
    ‘fields’ => [‘category’,
    ‘type_of_session’,
    ‘wheelchair_access’,
    ‘telephone_life_coaching’,
    ‘face_to_face_life_coaching’,
    ‘home_visits’,
    ‘sign_language’,
    ],
    ‘form’ => false
    ])
    ?>
    </div>
    </div>
    Ajax

    <script>
    jQuery(function($){
    $(‘.filter_list input’).on(‘change’, function()
    {
    var url = ‘<?php echo admin_url( ‘admin-ajax.php’ ); ?>?action=find_coach’;
    $(‘.filter_list input’).each(function()
    {
    var thisVal, metaKey;
    if($(this).prop(‘checked’) == true)
    {
    thisVal = $(this).val();
    metaKey = $(this).closest(‘div.acf-field’).data(‘name’);
    url += ‘&acf[‘+metaKey+’][]=’+thisVal;
    }

    });

    $.get(url, function(res){
    $.each(res, function(key, val){
    $user_row = ‘<div data-template=”member” class=”member”>\
    <div class=”user”>\
    \
    <div class=”article_left”>\
    <div class=”u-profile-picture”>\
    \
    </div>\
    </div>\
    <div class=”article_right”>\
    <div class=”profile-display-address”>\
    <h2 class=”member-name”>\
    ‘+val.display_name+’\
    </h2>\
    <div class=”col-6 address”>’+val.address+’,’+val.pin_code+'</div>\
    <div class=”col-6 “>’+val.bio+’
    </div>\
    </div>\
    </div>\
    <div class=”button-profile”>View profile</div>\
    </div>’;
    document.getElementById(‘member_list’).innerHTML = $user_row;
    });
    });
    });

    $(‘.filter_list input’).each(function(){
    //checkbox handling
    if($(this).prop(‘tagName’) == ‘INPUT’ && $(this).attr(‘type’) == ‘checkbox’){
    var val = getParamFromUrl($(this).attr(‘name’))
    if(val.indexOf($(this).val()) > -1){
    $(this).prop(‘checked’, true);
    }
    }
    });
    });

    function getParamFromUrl(name){
    var url = new URL(window.location.href);
    var c = url.searchParams.getAll(name);
    return c;
    }
    And query

    add_action(‘wp_ajax_find_coach’, ‘searchLifeCoach’);
    add_action(‘wp_ajax_nopriv_find_coach’, ‘searchLifeCoach’);
    function searchLifeCoach(){
    //print_R($_GET);
    $metaData = [];

    $fields = acf_get_fields($field_group_key);
    if(isset($_GET[‘acf’]) && !empty($_GET[‘acf’])){
    foreach($_GET[‘acf’] as $key => $field){
    $metaData[] = [
    ‘key’ => $key,
    ‘value’ => $field,
    ‘compare’ => is_array($field) ? ‘LIKE’ : ‘=’
    ];
    }
    }

    $args = array (
    ‘order’ => ‘ASC’,
    ‘orderby’ => ‘display_name’,
    ‘role__not_in’ => ‘administrator’,
    //’fields’ => ‘all_with_meta’,
    ‘meta_query’ => array(
    ‘relation’ => ‘OR’,
    array(
    ‘key’ => ‘pin_code’,
    ‘value’ => sanitize_text_field($_GET[‘pin_code’]),
    ‘compare’ => ‘=’
    ),
    array(
    ‘key’ => ‘city’,
    ‘value’ => sanitize_text_field($_GET[‘pin_code’]),
    ‘compare’ => ‘=’
    ),
    array(
    ‘key’ => ‘county’,
    ‘value’ => sanitize_text_field($_GET[‘pin_code’]),
    ‘compare’ => ‘LIKE’
    ),
    array(
    ‘key’ => ‘county_2’,
    ‘value’ => sanitize_text_field($_GET[‘pin_code’]),
    ‘compare’ => ‘LIKE’
    ),
    array(
    ‘key’ => ‘category’,
    ‘value’ => sanitize_text_field($_GET[‘category’]),
    ‘compare’ => ‘LIKE’
    )
    ),

    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    $metaData
    )
    );

    // Create the WP_User_Query object
    $wp_user_query = new WP_User_Query( $args );
    //echo ‘

    '; print_r($wp_user_query); echo '

    ‘;
    // Get the results
    $authors = $wp_user_query->get_results();

    foreach($authors as $author){
    $userMeta = get_user_meta($author->data->ID);
    //echo ‘

    '; print_r($userMeta); echo '

    ‘;
    $biofull = (isset($userMeta[‘coach_bio’]) ? $userMeta[‘coach_bio’][0] : ”);
    $biotext = substr_replace($biofull, “…”, 200);
    $bio = strip_tags($biotext);
    $userList[] = [
    ‘uid’ => $author->data->ID,
    ‘display_name’ => $author->data->display_name,
    ’email’ => $author->data->user_email,
    ‘user_url’ => $author->data->user_url,
    ‘avatar’ => (isset($userMeta[‘avatar’]) ? wp_get_attachment_url($userMeta[‘avatar’][0]) : ”),
    ‘first_name’ => (isset($userMeta[‘first_name’]) ? $userMeta[‘first_name’][0] : ”),
    ‘last_name’ => (isset($userMeta[‘last_name’]) ? $userMeta[‘last_name’][0] : ”),
    ‘phone’ => (isset($userMeta[‘phone’]) ? $userMeta[‘phone’][0] : ”),
    ‘address’ => (isset($userMeta[‘address’]) ? $userMeta[‘address’][0] : ”),
    ‘address_2’ => (isset($userMeta[‘address_2’]) ? $userMeta[‘address_2’][0] : ”),
    ‘pin_code’ => (isset($userMeta[‘pin_code’]) ? $userMeta[‘pin_code’][0] : ”),
    ‘bio’ => (isset($userMeta[‘coach_bio’]) ? $userMeta[‘coach_bio’][0] : ”),
    ‘city’ => (isset($userMeta[‘city’]) ? $userMeta[‘city’][0] : ”),
    ‘city_2’ => (isset($userMeta[‘city_2’]) ? $userMeta[‘city_2’][0] : ”),
    ‘pin_code_2’ => (isset($userMeta[‘pin_code_2’]) ? $userMeta[‘pin_code_2’][0] : ”),
    ‘category’ => (isset($userMeta[‘category’]) ? $userMeta[‘category’][0] : ”),
    ‘type_of_session’ => (isset($userMeta[‘type_of_session’]) ? $userMeta[‘type_of_session’][0] : ”),
    ‘wheelchair_access’ => (isset($userMeta[‘wheelchair_access’]) ? $userMeta[‘wheelchair_access’][0] : ”),
    ‘sign_language’ => (isset($userMeta[‘sign_language’]) ? $userMeta[‘sign_language’][0] : ”),
    ‘bio’ => $bio,
    ];
    }
    return wp_send_json($userList);
    exit();
    }

Viewing 1 post (of 1 total)

The topic ‘Filter by user meta with checkbox not working’ is closed to new replies.