Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to build a search form that will filter users based on their custom fields. These custom fields are things like 'company_name' 'user_country' etc. I have some code and I am having a small amount of success with things so far. I can search for cities for example, but I am not sure if these cities are actually relating to the city names I have created.

I think it's better if I show a code example:

<?php
function aio_search_meta_data($atts, $content = null) {
global $wpdb;

extract(shortcode_atts(array(
    "role" => '',
    "number" => 3,
    ), $atts));

// Search general terms 
$search_term = (isset($_GET['aio_keyword_search'])) ? sanitize_text_field($_GET['aio_keyword_search']) : false;

$number = sanitize_text_field($number);
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$offset = ($page - 1) * $number;
// Set up the Query
if($search_term) {
$meta_query_args = array(
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'user_city',
            'value' => $search_term,
        ),
        array(
            'key' => 'user_country',
            'value' => $search_term,
        ),
        array(
            'key' => 'user_company',
            'value' => $search_term,
        ),
    ));
// Instantiate a new object 
$meta_query = new WP_Meta_Query($meta_query_args);
// Retrieve SQL
$mq_sql = $meta_query->get_sql(
    'user',
    $wpdb->users,
    'ID',
    null
); 

} else {
$meta_query_args = new WP_User_Query(
    array(
        'role' => '',
        'offset' => $offset,
        'number' => $number,
        'meta_query' => array(
            'key' => 'user_country',
            'value' => ''
        ),
        'meta_key' => 'company_name',
        'orderby' => 'meta_value',
        'order' => 'ASC'
    ));

} ?>

<form class="member-directory-listings" method="get" action="<?php the_permalink(); ?>" role="form">
<div class="row">
    <div class="col-md-12">
        <div class="form-group">
            <input type="text" name="aio_keyword_search" id="aio_keyword_search" class="form-control" placeholder="Enter Keywords">
            <span class="assitive-text">Search for network partners by country, city, or company name</span>
        </div>
    </div><!-- .col-md-12 -->
</div><!-- .row -->

<div class="row">
    <div class="form-group">
        <div class="col-md-4">
            <select class="form-control" name="aio_countries">
                <option value="">Value</option>
            </select>
        </div>
        <div class="col-md-4">
            <select class="form-control" name="aio_countries">
                <option value="">Value</option>
            </select>
        </div>
        <div class="col-md-4">
            <select class="form-control" name="aio_countries">
                <option value="">Value</option>
            </select>
        </div>
    </div><!-- .col-md-4 -->
</div><!-- .row -->
<input type="submit" class="grey-btn" value="Search">
</form>


<?php 
if(!empty($mq_sql)) { ?>
<div class="table-responsive">
<table class="table table-striped">
    <thead class="aio-members-directory">
        <tr>
            <th>Logo</th>
            <th>Company Name</th>
            <th>City</th>
            <th>Country</th>
        </tr>
    </thead>
    <tbody>

<?php
foreach($mq_sql as $mq) {
    $args = array(
        'role' => '',
    );
    $author_info = new WP_User_Query($args); 
    $author_details = get_userdata($mq->ID);?>
    <tr>
        <td class="member-logo-image">
            <img class="member-profile-img small-image-bg" src="<?php echo $author_details->image; ?>" alt="User image" />
        </td>
        <td>
        <a href="<?php echo get_author_posts_url($mq->ID); ?>"><?php echo $author_details->company_name; ?></a>
        </td>
        <td>
            <?php echo $author_details->user_city; ?>
        </td>
        <td>
            <?php echo $author_details->user_country; ?>
        </td>
        <?php }  ?>
    </tr>
    </tbody>
</table><!-- .author-list -->
</div><!-- . table-responsive -->

I have to complete this project by Friday. Can anyone help me to put this form together? I need to be able to create drop downs too with predefined information.

share|improve this question
    
" I am not sure if these cities are actually relating to the city names I have created" -- I don't know if this can be answered without access to your data, and without a lot of time analyzing it. –  s_ha_dum May 19 at 14:21
    
What is your actual question here? How does your code not work or not work as you imagine it should? –  Rarst May 19 at 16:24
    
Firstly I don't know how to return any results, so if for example a do a search for Bangkok, nothing comes up. I'm obviously way off the mark here and I only have until Friday to figure this out. –  user3269469 May 20 at 0:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.