0

I currently have the following code, and a user named "Clark Farm" in the user_business table. The code below returns nothing. If I remove the search term then all of the users are listed. I'm trying to determine why the search is not working..

<?php
/**
 * @package WordPress
 * @subpackage themename
 */

get_header(); ?>
<?php 
    $url = $_SERVER['REQUEST_URI'];
    $tokens = explode('/', $url);
    $search = $tokens[sizeof($tokens)-1];
    $searchQuery = str_replace("-", " ", $search);
?>
<?php echo $search; ?>
<div id="main">
        <div id="primary">
            <div id="content">
                <?php the_post(); ?>
                <div class="contentLeft">

                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?> role="article">

                        <h1 class="entry-title">Search Results for: <?php echo $searchQuery; ?></h1>

                    <div class="entry-content">

                        <?php 
                            // WP_User_Query arguments
                            $args = array (
                                'search'         => 'Clark',
                                'search_columns' => array( 'user_business' ),
                                'count_total'    => true,
                                'fields'         => 'all_with_meta',
                            );

                            // The User Query
                            $user_query = new WP_User_Query( $args );

                            // The User Loop
                            if ( ! empty( $user_query->results ) ) {
                                foreach ( $user_query->results as $user ) {
                                    echo $user->user_business . '<br>';
                                }
                            } else {
                                    echo 'NOT WORKING!';
                            }
                        ?>

                    </div><!-- .entry-content -->
                </article><!-- #post-<?php the_ID(); ?> -->

                </div>
                <div class="contentRight"><?php if ( dynamic_sidebar('main-sidebar') ) : else : ?><?php endif; ?></div>
                </div>
            </div><!-- #content -->
        </div><!-- #primary -->
<?php get_footer(); ?>
  • What is user_business? – s_ha_dum Jul 2 '13 at 17:05
  • That is, it sounds like you have a custom table and maybe a user meta data involved. – s_ha_dum Jul 2 '13 at 17:08
  • yes, user_business is a custom table, are those not permitted when using wp user query? – Austin Biggs Jul 2 '13 at 17:11
1

WP_User_Query searches the $wpdb->users table. It will not join on your custom table. How would it know what to JOIN? The possible tables names and structures are practically infinite.

I believe you might be able to use a filter on pre_user_query to insert your own values in the WHERE clause but there is no JOIN value so you will have to alter query_from and query_where. It could be tricky but should be do-able.

Edit:

Given new information, it now sounds like what you need is a meta_query.

$args = array(
    'count_total'    => true,
    'fields'         => 'all_with_meta',
    'meta_query' => array(
        array(
            'key' => 'user_business',
            'value' => 'Clark',
            'compare' => '='
        )
    )
 );
$user_query = new WP_User_Query( $args );

That assumes that your user meta key is user_business and that you want an exact match. See the Codex for other values for compare.

  • Isn't User Meta kept within the Users table? – Austin Biggs Jul 2 '13 at 17:23
  • User meta is kept in the $wpdb->usermeta table but that isn't relevant since you told me that "user_business is a custom table". – s_ha_dum Jul 2 '13 at 17:25
  • Sorry, must've used the wrong word. user_business is a custom meta field for users, stored the same way as all meta. – Austin Biggs Jul 2 '13 at 17:39

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.