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.

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.

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

get_header(); ?>
<?php 
    $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">

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

2 Answers 2

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 (
                                    array(
                                        'key' => '_start_date',
                                        'value' => $_start_of_month,
                                        'compare' => '>',
                                    ),
                                    array(
                                        '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

 
discard

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.