3

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(); ?>

2 Answers 2

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.

1
  • 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, 2013 at 20:32
0

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.

2
  • 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. Jul 22, 2013 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. Jul 23, 2013 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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