Title: Query posts by custom fields value
Last modified: August 19, 2016

---

# Query posts by custom fields value

 *  Resolved [eian00](https://wordpress.org/support/users/eian00/)
 * (@eian00)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/query-posts-by-custom-fields-value/)
 * Hello
 * can I somehow display posts that satisfy the custom field values that I need:
 * for ex, display all posts that have the value A from the field X, and value B
   from the field Y
 * and I need to display them only if they have both the values i need.
 * I was using the code below, but it shows posts that have values A or B, but I
   need to display them just if they have exactly the two i specify in the query
   loop.
 *     ```
       <?php query_posts('meta_key=color&meta_value=black
       &meta_key=shape&meta_value=circle&orderby=title&order=asc');  ?>
   
         <?php while (have_posts()) : the_post(); ?>
   
        <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>&nbsp;,&nbsp;
   
         <?php
          global $wp_query;
          $postid = $wp_query->post->ID;
          echo get_post_meta($postid, 'color', true);
         ?>
   
         <?php endwhile;?>
       ```
   
 * How to filter that?
 * thank you

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/query-posts-by-custom-fields-value/#post-1596577)
 * The `WP_Query` class (what `query_posts` also uses) does not support querying
   for multiple meta keys.
 * For multiple meta keys you’ve have to write your own query, see the wpdb class.
   
   [http://codex.wordpress.org/Function_Reference/wpdb_Class](http://codex.wordpress.org/Function_Reference/wpdb_Class)
 * Or you could do it this way, using a combination of the two (wpdb + query_posts/
   wp_query).
 *     ```
       // Get the IDs for posts with both meta keys and values
       $posts_with_meta = $wpdb->get_col("
       	SELECT ID from $wpdb->posts p
       		JOIN $wpdb->postmeta meta1 ON meta1.post_id = p.ID
       		JOIN $wpdb->postmeta meta2 ON meta2.post_id = p.ID
       	WHERE p.post_status = 'publish'
       	AND p.post_type = 'post'
       	AND meta1.meta_key = 'apples'
       	AND meta2.meta_key = 'oranges'
       	AND meta1.meta_value = 'juicy'
       	AND meta2.meta_value = 'sweet'
       ");
       // Create query object
       $my_query = new WP_Query();
       // Setup query paramters
       $my_query->query( array( 'post__in' => $posts_with_meta ) );
   
       // The above two lines could be replaced with the below instead
       // query_posts(  array( 'post__in' => $posts_with_meta )  );
       ```
   
 * You could still plonk in your own parameters that way, ie. category, tag etc,
   as you usually would, we just use a custom query to go fetch the post IDs for
   posts with the relevant meta data first, then add that array of IDs into the `
   post__in` parameter to narrow down the results to only those posts.
 *  Thread Starter [eian00](https://wordpress.org/support/users/eian00/)
 * (@eian00)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/query-posts-by-custom-fields-value/#post-1596894)
 * ok nice, i found something out, the code i found is working just for one field,
   how can i modify the code below to join two fields and two values?
 *     ```
       <?php
       global $wpdb;
       global $post;
        $querystr = "
           SELECT wposts.*
           FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
   
           WHERE wposts.ID = wpostmeta.post_id
           AND wpostmeta.meta_key = 'name'
           AND wpostmeta.meta_value = 'john'
           AND wpostmeta.meta_key = 'age'
           AND wpostmeta.meta_value = '26'
           AND wposts.post_type = 'post'
           ORDER BY wpostmeta.meta_value DESC
           ";
   
       $pageposts = $wpdb->get_results($querystr, OBJECT);
   
       ?>
       ```
   
 * any idea?
 *  [arpitap](https://wordpress.org/support/users/arpitap/)
 * (@arpitap)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/query-posts-by-custom-fields-value/#post-1596922)
 * Hey! Mark,
 * Thnaks for the solution, it worked for me.
 * Thnaks a lot.
 * Arpita
 *  [mariostella](https://wordpress.org/support/users/mariostella/)
 * (@mariostella)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/query-posts-by-custom-fields-value/#post-1596937)
 * @ Mark / t31os
    I tried your query but it does not output anything the way it
   is, whereas if I use the `query_posts( array( 'post__in' => $posts_with_meta ));`
   I get an infinite repetition of a specific post (dunno why that one in particular
   as it does not even satisfy the query parameters).
 * Any idea? I am on 2.9.2

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Query posts by custom fields value’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 4 participants
 * Last reply from: [mariostella](https://wordpress.org/support/users/mariostella/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/query-posts-by-custom-fields-value/#post-1596937)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
