Title: Multiple custom field based query
Last modified: August 20, 2016

---

# Multiple custom field based query

 *  [sheme](https://wordpress.org/support/users/sheme/)
 * (@sheme)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/multiple-custom-field-based-query/)
 * Hi everybody,
 * I know this questions have been asked many times, but I can’t get my code work;-(
 * I’m trying to filter a listing aof posts with a query base on two custom fields.
 * So here’s my code :
 *     ```
       $today = date( 'd-m-Y' );
                       $where .= " AND ($wpdb->posts.ID in (
                       select $wpdb->postmeta.post_id from $wpdb->postmeta
                       JOIN $wpdb->postmeta meta1 ON meta1.post_id = p.ID
       		JOIN $wpdb->postmeta meta2 ON meta2.post_id = p.ID
                       WHERE meta1.meta_key = 'end_date'
       	        AND meta2.meta_key >='".$today."'
       	        AND meta1.meta_value = '2-3-ans'
       	        AND meta2.meta_value = 'on'))";
                       return $where;
       ```
   
 * Surely there’s something I miss because it’s not working ;-(
 * The original code was
 * `$where .= " AND ($wpdb->posts.ID in (select $wpdb->postmeta.post_id from $wpdb-
   >postmeta where $wpdb->postmeta.meta_key='end_date' and $wpdb->postmeta.meta_value
   >='".$today."')) ";`
 * and works
 * Thanks in advance guys for helping

Viewing 1 replies (of 1 total)

 *  [marfarma](https://wordpress.org/support/users/marfarma/)
 * (@marfarma)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-custom-field-based-query/#post-2333397)
 * The last time I did something like this it turned out to be that the date wasn’t
   formatted the way I thought it was. The best place to start with something like
   this is to look at the actual sql statement that’s being build by examining the
   value of
 * $wp_query->request
 * If you install the [developer tools plugin](http://wordpress.org/extend/plugins/developer-tools/),
   you can use
 * `krumo($wp_query->request );`
 * in place of echo and it will give you a nicer presentation of the results. If
   you can’t figure out what the sql statement is doing, paste it back here and 
   we’ll see if we can help.

Viewing 1 replies (of 1 total)

The topic ‘Multiple custom field based query’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [marfarma](https://wordpress.org/support/users/marfarma/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/multiple-custom-field-based-query/#post-2333397)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
