WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How would I modify the following SQL Query to allow me to search multiple meta keys at once? e.g. user_business AND user_about

In the end I'll be searching about 40 - 50 different meta keys; or if there's an option to search ALL of them, that'd be even better.

 * @package WordPress
 * @subpackage themename

get_header(); ?>
    $url = $_SERVER['REQUEST_URI'];
    $tokens = explode('/', $url);
    global $search;
    $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">

                        global $wpdb;
                        $usermeta = $wpdb->prefix . 'usermeta';

                        // get id of user
                        $select_user = "SELECT user_id FROM $usermeta WHERE meta_key = 'user_business' AND meta_value LIKE '%$searchQuery%'";
                        $user_id = $wpdb->get_var($select_user);
                        echo $user_id;

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

                <div class="contentRight"><?php if ( dynamic_sidebar('main-sidebar') ) : else : ?><?php endif; ?></div>
            </div><!-- #content -->
        </div><!-- #primary -->
<?php get_footer(); ?>
share|improve this question

You'll want to use the 'meta_query' argument of WP_Query ( http://codex.wordpress.org/Class_Reference/WP_Query )

Here's a snippet that uses two separate meta key comparisons:

$query_args = array(
    'post_type' => 'event',
    'order' => 'ASC',
    'orderby' => 'meta_value_num',
    'meta_key' => '_start_date',
    'meta_query' => array (
                                        'key' => '_start_date',
                                        'value' => $_start_of_month,
                                        'compare' => '>',
                                        'key' => '_start_date',
                                        'value' => $_end_of_month, 
                                        'compare' => '<',


Important: Note that meta_query takes an array of arrays.

share|improve this answer
With 40 or 50 meta keys to query, you might want to build the array separately, then do something like ...'meta_query' => $my_meta_query,... – Pat J Jul 2 '13 at 20:32
up vote 0 down vote accepted

Actually the way I solved this issue was instead of supplying a key name, I left the field blank which returned worked for all meta keys.

share|improve this answer
Well, it sounds like you never really wanted to "search" in the first place. Rather, you wanted to simply fetch metadata, which is a totally different question. Either way - glad you've got something working for you. – MadtownLems Jul 22 '13 at 18:17
The code I wrote searches all of the meta keys for the supplied value. It doesn't simply fetch all of the meta data. – AustinTBiggs Jul 23 '13 at 20:41

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.