WordPress.org

Ready to get started?Download WordPress

Forums

query posts with array of possible meta data values? (2 posts)

  1. Stephen S
    Member
    Posted 4 years ago #

    Hi, I am pulling my hair out trying to figure this out: I want to construct a query that will return posts whose meta values match an array of possibilities. Here is what I have been trying:

    $people_query = new WP_Query('category_name=who-we-are&meta_key=first_name&array("meta_value"=> array("john","sam"))&orderby=title&order=ASC');

    but to no avail. I have tried various combos of quotes, tried adding "__and" to "meta_value" to simulate multiple category array, among other things. The query returns the full list of everyone in the category, no matter what I do. I only want it to return the list of people whose names match those in the array.

    Any ideas?

  2. vtxyzzy
    Member
    Posted 4 years ago #

    Give this a try:

    function mam_posts_where ($where) {
       global $mam_global_where;
       if ($mam_global_where) $where .= " $mam_global_where";
       return $where;
    }
    add_filter('posts_where','mam_posts_where');
    $mam_global_where = " AND $wpdb->postmeta.meta_value IN ('john','sam')";
    $people_query = new WP_Query('category_name=who-we-are&meta_key=first_name&orderby=title&order=ASC');

Topic Closed

This topic has been closed to new replies.

About this Topic