I am kinda stuck with the following issue.
I have a search page on my website using wordpress (at the moment still local), with the possibility of using a filter.

So in the end the select query should like something like (not the actual table and stuff, but just to make clear how the select should look like):

SELECT * FROM users WHERE 1=1 AND city LIKE %SomeCity% AND (sports LIKE %somesports1% OR sports LIKE %somesports2% OR sports LIKE %somesports3%) AND stype LIKE %sType%;

I am trying to do this through the wp_user_query (which combines already with user_meta), it gets the values to look for from the url via $_GET, and this is what I have sofar:

$query_array = array();
$args = array(
    'exclude' => 1,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key'       => 'sent_activation_status',
            'value'     => 'ACTIVE',
            'compare'   => '='
        ),
    )
);

if (isset($_GET['sports']) && $_GET['sports'] != NULL) {
    $query = explode('&', $_SERVER['QUERY_STRING']);
    $sports_query = array();
    foreach ($query as $param) {
        list($name, $value) = explode('=', $param, 2);
        if ($value !== '') {
            if ($name === 'sports') {
                $sports_query2 = $value . ',';
            }       
        }
    }
    $sports_query2 = rtrim($sports_query2,',');
    $mySportsArray = explode(',', $sports_query2);
    foreach ($mySportsArray as $value) {
        $query_sports = array(
            'key' => 'user_sports',
            'value' => $value,
            'compare' => 'LIKE'
        );
    }
    if (count($query_sports) > 1) {
        $query_sports['relation'] = 'OR';
    }
    array_push($query_array, $query_sports);
}

if (isset($_GET['city']) && $_GET['city'] != NULL) {
    $query_city = array(
        'key' => 'user_city', 
        'value' => $_GET['city'], 
        'compare' => 'LIKE'
    );
    array_push($query_array, $query_city);
}

if(count($query_array) >= 1) {
    $query_array['relation'] = 'OR';
    $args['meta_query'] = $query_array;
}

$user_search_query = new WP_User_Query( $args );

$users = $user_search_query->get_results();

return $users;

But this doesn’t seem correct.
Do I need to create a custom select query instead of simply using the way I am doing now?

Thanks for any help.

Cheers,

Read more here: Very custom wp_user_query


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: