Title: Sql with multiple post_meta statements
Last modified: August 20, 2016

---

# Sql with multiple post_meta statements

 *  Resolved [MvdP](https://wordpress.org/support/users/mvdp/)
 * (@mvdp)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/sql-with-multiple-post_meta-statements/)
 * Hi all,
 * I need to query my WordPress db based on the posts metadata. No special tricks
   there. Unfortunately though I need to match for 2 specific statements. Here is
   my, not working, query so far:
 *     ```
       SELECT
       	*
       FROM
       	wp_postmeta
       WHERE
       	(
       			(meta_key = 'lat' AND meta_value >= '60.23457047672217')
       		OR
       			(meta_key = 'lat' AND meta_value <= '60.23457047672217')
       	)
       	AND
       	(
       			(meta_key = 'long' AND meta_value >= '24.879140853881836')
       		OR
       			(meta_key = 'long' AND meta_value <= '24.879140853881836')
       	)
       GROUP BY
       	post_id
       ```
   
 * If I remove the “AND” part everything goes well and I get results back only based
   on the latitude or longitude values. I need to match on both values however.
 * How can I accomplish this?
 * Thanks in advance.

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

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/sql-with-multiple-post_meta-statements/#post-2471704)
 * Have you looked at meta_query?
 * [http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters](http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters)
 * Why do you need the “OR” statement when both values are the same?
 *  Thread Starter [MvdP](https://wordpress.org/support/users/mvdp/)
 * (@mvdp)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/sql-with-multiple-post_meta-statements/#post-2471707)
 * Yes I have looked at the meta_query.
 * My example query was a bit wrong it should be something like this i think:
 * `SELECT
    wp_posts.* INNER JOIN wp_postmeta FROM wp_postmeta WHERE ( (meta_key
   = ‘lat’ AND meta_value >= ’55’) OR (meta_key = ‘lat’ AND meta_value <= ’65’) )
   AND ( (meta_key = ‘long’ AND meta_value >= ’20’) OR (meta_key = ‘long’ AND meta_value
   <= ’30’) ) GROUP BY post_id’
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/sql-with-multiple-post_meta-statements/#post-2471742)
 * see if this meta_query works [untested]:
 *     ```
       <?php
       $args = array(
   
        	'meta_query' => array(
       		'relation' => 'AND',
        		array(
        			'key' => 'lat',
        			'value' => array( 55, 65 ),
        			'type' => 'numeric',
        			'compare' => 'BETWEEN'
        		),
        		array(
        			'key' => 'long',
        			'value' => array( 20, 30 ),
        			'type' => 'numeric',
        			'compare' => 'BETWEEN'
        		)
        	)
         );
        $query = new WP_Query( $args );
   
        ?>
       ```
   
 *  Thread Starter [MvdP](https://wordpress.org/support/users/mvdp/)
 * (@mvdp)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/sql-with-multiple-post_meta-statements/#post-2471763)
 * [@keesiemeijer](https://wordpress.org/support/users/keesiemeijer/) you are my
   hero!
 * Sometimes the answer can be so simple… yet so hard. Totally over thought this
   whole thing. Sometimes it’s the simpleness that does the job!
 * Thanks a lot!!
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/sql-with-multiple-post_meta-statements/#post-2471765)
 * No problem. Glad you got it resolved.
 *  Thread Starter [MvdP](https://wordpress.org/support/users/mvdp/)
 * (@mvdp)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/sql-with-multiple-post_meta-statements/#post-2471771)
 * If one is interested in the produced final query it looks like this:
 *     ```
       SELECT
       	SQL_CALC_FOUND_ROWS wp_posts.*
       FROM
       	wp_posts
       INNER JOIN
       	wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
       INNER JOIN
       	wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
       WHERE
       	1=1
       AND
       	wp_posts.post_type = 'post'
       AND
       	(wp_posts.post_status = 'publish')
       AND
       	( (wp_postmeta.meta_key = 'lat' AND CAST(wp_postmeta.meta_value AS SIGNED) BETWEEN '55' AND '65')
       AND
       	(mt1.meta_key = 'long' AND CAST(mt1.meta_value AS SIGNED) BETWEEN '20' AND '30') )
       GROUP BY
       	wp_posts.ID
       ORDER BY
       	wp_posts.post_date
       DESC LIMIT 0, 10
       ```
   
 *  [RSimpson](https://wordpress.org/support/users/rsimpson/)
 * (@rsimpson)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/sql-with-multiple-post_meta-statements/#post-2471997)
 * I’m trying to do the same thing, but it always (without fail it appears) returns
   no posts when the values in the array being compared are negative.
 * Any ideas?

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

The topic ‘Sql with multiple post_meta statements’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 3 participants
 * Last reply from: [RSimpson](https://wordpress.org/support/users/rsimpson/)
 * Last activity: [13 years, 6 months ago](https://wordpress.org/support/topic/sql-with-multiple-post_meta-statements/#post-2471997)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
