0

I'm struggling to apply some filters to WP_User_Query, I have the following query, followed by some filters also shown, when I apply the filters they appear within the url structure, so /?gender=Male but I'm struggling to connect the filters to what is actually output, is there a simple method to take what I have and apply the filters to the array? thanks

<?php
$args  = array(
'role'      => 'talent',
'order'     => 'DESC',
'orderby'   => 'user_registered',
'meta_key'  => 'position',
'number'    => '15',
    'meta_query'=> array (
        array(
            'key'       => 'picture',
            'value'   => array(''),
            'compare' => 'NOT IN'
        ),
        array(
            'key'       => 'position',
            'value'     => $title
        )
    )
    );
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
if (!empty($authors))
{
foreach ($authors as $author)
{
?>

<?php include(locate_template('profile.php')); ?>

<?php } ?>
<?php } else { ?>
<?php } ?>

Filters

<form>

<strong>
    Age
</strong>

<span>
    <select name="age">
    <?php
    $orderby_options = array(
    '10' => '10',
    '20' => '20',
    '30' => '30',
    );
    foreach( $orderby_options as $value => $label ) {
    echo "<option ".selected( $_GET['age'], $value )." value='$value'>$label</option>";
    }
    ?>
    </select>
</span>

<strong>
    Gender
</strong>

<span>
    <select name="gender">
    <?php
    $orderby_options = array(
    'All' => 'All',
    'Male' => 'Male',
    'Female' => 'Female',
    );
    foreach( $orderby_options as $value => $label ) {
    echo "<option ".selected( $_GET['gender'], $value )." value='$value'>$label</option>";
    }
    ?>
</span>

<input type='submit' value='Search'>

</form>
  • 1
    What filters? I don't understand this question. – s_ha_dum May 8 '15 at 15:07
  • Hi @s_ha_dum so the form select fields are named based of custom user meta, so "age" & "gender" are both keys I was hoping could be used to filter the results, so if Male was selected as the Gender, it would reload the page when submitted and refine the results based on Male users if that makes sense. I'm just not sure how to go about connecting the form with the query on the page. – Farting Santa May 8 '15 at 15:29
1

Mostly a PHP question but if I understand your question, you want something like this:

$args  = array(
'role'      => 'talent',
'order'     => 'DESC',
'orderby'   => 'user_registered',
'meta_key'  => 'position',
'number'    => '15'
);
$meta = array (
    array(
        'key'       => 'picture',
        'value'   => array(''),
        'compare' => 'NOT IN'
    ),
    array(
        'key'       => 'position',
        'value'     => $title
    )
);

$orderby_options = array(
  'All' => 'All',
  'Male' => 'Male',
  'Female' => 'Female',
);
//  var_dump($_GET);
if (!empty($_GET['gender']) && in_array($_GET['gender'],$orderby_options)) {

 $meta[] = array(
   'key' => 'gender',
   'value' => $orderby_options[$_GET['gender']]
 );
}

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.