Search filter by form

Demo
implimented in “http://estategrid.com/find-agent/
This is form on submit button

<form method=”post” action=””  name=”agent” enctype=”multipart/form-data”>
<?php
/* sidebar for agent form*/

//list each role and each user with that role
global $wp_roles;
?><select name=”myagent”><option value=””>Select Agent</option><?php
foreach( $wp_roles->role_names as $role => $name ) {
$name = translate_with_context($name);
if($name == ‘Agent’){
$this_role = “‘[[:<:]]”.$role.”[[:>:]]'”;
$query = “SELECT * FROM $wpdb->users WHERE ID = ANY (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = ‘wp_capabilities’ AND meta_value RLIKE $this_role) ORDER BY user_nicename ASC LIMIT 10000″;
$users_of_this_role = $wpdb->get_results($query);
if ($users_of_this_role) {
foreach($users_of_this_role as $user) {
$curuser = get_userdata($user->ID);
$author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
$agent = $curuser->user_nicename ;
echo ‘<option value=”‘.$agent.'”>’.ucfirst($agent).'</option>’;
}
}
}
}
?>
</select>
<?php
global $wpdb;

$argsa =array( ‘role’ => ‘aamrole_53673234ba574 ‘ );
$a = get_users( $argsa );
$argsb =array( ‘role’ => ‘ aamrole_5329589cf0296 ‘ );
$b = get_users( $argsb );
$agentusers = array_merge( $a, $b );
#print_r($agentusers);

foreach($agentusers as $agentuser)
{
$user_id = $agentuser->ID;
$agentlocation = get_user_meta($user_id,’location’,true);
//$agentlocation = $wpdb->get_var(“SELECT meta_value FROM “.$wpdb->usermeta.” WHERE meta_key = ‘location’ and user_id=$user_id”);

if(!empty($agentlocation))
$location_array[]=strtolower($agentlocation);
}
$mylocation_array= array_unique($location_array);
if(!empty($mylocation_array)){
?><select name=”mylocation”>
<option value=””>Select Location</option>
<?php
foreach($mylocation_array as $mylocation=>$value)    {
echo ‘<option value=”‘.$value.'”>’.ucfirst($value).'</option>’;
}?>
</select><?php
}
?>
<input type=”submit” value=”Refine Results” name=”agent”>
<?php
/* end sidebar for widget form*/
?>
</form>

After submit button

<?php /* after submit form */
if (isset($_POST[‘agent’]))
{
$args = “”;
$myagent = $_POST[‘myagent’];
$mylocation = $_POST[‘mylocation’];
if(!empty($myagent) && !empty($mylocation))
{
$args = array(
‘meta_query’ => array(
‘relation’ => ‘AND’,
array(
‘key’     => ‘nickname’,
‘value’   => $myagent,
‘compare’ => ‘LIKE’
),
array(
‘key’     => ‘location’,
‘value’   => $mylocation,
‘compare’ => ‘LIKE’
),
),

);

}else if(!empty($myagent) && empty($mylocation))
{
$args = array(
‘meta_query’ => array(

array(
‘key’     => ‘nickname’,
‘value’   => $myagent,
‘compare’ => ‘LIKE’
),

),

);
}
else if(empty($myagent) && !empty($mylocation))
{
$args = array(
‘meta_query’ => array(

array(
‘key’     => ‘location’,
‘value’   => $mylocation,
‘compare’ => ‘LIKE’
),

),

);
}
if(!empty($args))
{
$users = new WP_User_Query( $args);
$users = $users->get_results();

foreach($users as $user)
{
?>
<div class=”agent-detail”>
<div class=”box”>
<div class=””><?php echo  get_avatar( $user->user_email); ?></div>
<h6 class=”service-title”>Name: <?php echo $user->nickname; ?></h6>
<h6 class=”service-title”>Location: <?php echo $user->location; ?></h6>
<h6 class=”service-title”>Email: <?php echo $user->user_email; ?></h6>
<h6 class=”service-title”>Mobile Phone: <?php echo $user->phone_mobile; ?></h6>
<h6 class=”service-title” style=”margin-bottom: 7px;”>About this Agent:</h6>
<p><?php echo $user->description; ?></p>
</div>
</div>
<?php
}
}
}
else
{
global $wpdb;
$argsa =array( ‘role’ => ‘aamrole_53673234ba574 ‘ );
$a = get_users( $argsa );
$argsb =array( ‘role’ => ‘ aamrole_5329589cf0296 ‘ );
$b = get_users( $argsb );
$agentusers = array_merge( $a, $b );
#print_r($agentusers);
foreach($agentusers as $agentuser)
{
?>
<div class=”agent-detail”>
<div class=”box”>
<div class=”box-img” style=”width:20%; float:left;”>
<?php echo  get_avatar( $agentuser->user_email); ?>
</div>
<div class=”box-data”>
<table border=1>
<tr><td>Name:</td><td><?php echo $agentuser->nickname; ?></td></tr>
<tr><td>Location:</td><td><?php echo $agentuser->location; ?></td></tr>
<tr><td>Email:</td><td><?php echo $agentuser->user_email; ?></td></tr>
<tr><td>Mobile Phone:</td><td><?php echo $agentuser->phone_mobile; ?></td></tr>
</table>
</div>
<div class=”box-desc”>
<br>About this Agent:
<p><?php echo $agentuser->description; ?></p>
</div>
</div>
</div>
<?php
}
} ?>

<?php
/* end submit form*/?>

Advertisements

Tagged: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: