Multiple meta key and meta value in single query (wordpress)

I am running a query successfully in wordpress.Query is as follows.

SELECT wp_posts.*, wp_postmeta.meta_value
FROM wp_posts, wp_postmeta, wp_term_relationships, wp_terms
WHERE term_id = '12'
  AND term_taxonomy_id = '12'
  AND ID = post_id
  AND ID = object_id
  AND post_type = 'property'
  AND post_status = 'publish'
  AND meta_key = 'property_amount'
  AND replace(replace(meta_value, ',', ''), '"', '') >= 1
GROUP BY wp_posts.ID
ORDER BY replace(replace(meta_value, ',', ''), '"', '') DESC LIMIT 0, 10

But I want to add one more meta_key and its value condition in above query so I changed my query to this

Read More
SELECT wp_posts.*, wp_postmeta.meta_value
FROM wp_posts, wp_postmeta, wp_term_relationships, wp_terms
WHERE term_id = '12'
  AND term_taxonomy_id = '12'
  AND ID = post_id
  AND ID = object_id
  AND post_type = 'property'
  AND post_status = 'publish'
  AND ((wp_postmeta.meta_key = 'property_amount'
      AND wp_postmeta.meta_value) >= '1'
      AND (wp_postmeta.meta_key = 'property_land_type'
      AND wp_postmeta.meta_value IN ('H', 'C')))
GROUP BY wp_posts.ID

Following line is extra in first query

meta_key="property_land_type" and meta_value in ('L','H','C')

But it is not working. How to do this.I can not write WP_Query this time as I have lots of other queries based on this query.

Thanks in advance!!!

Related posts

Leave a Reply

2 comments

  1. Without adding your own query, try to extend the wordpress query. You can customize the following code,

    $args = array(
        'post_type' => 'property',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'property_amount',
                'value' => '1',
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'property_land_type',
                'value' => 'L',
                'compare' => 'LIKE',
            )
        )
    );
    
    $query = new WP_Query( $args );
    
  2. $args = array(
        'post_type' => 'property',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'property_amount',
                'value' => '1',
                'compare' => '=',
            ),
            array(
                'key' => 'property_land_type',
                'value' => array('L','H','C'),
                'compare' => 'IN',
            )
        )
    );
    
    $query = new WP_Query( $args );
    
    if ( $query->have_posts() ) :
        while ($query->have_posts()) : $query->the_post();
                 echo $post_id = get_the_ID();
        endwhile;
    endif;