Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:35:33+00:00 2026-06-15T09:35:33+00:00

I am trying to write a query to check if the custom meta values

  • 0

I am trying to write a query to check if the custom meta values in a custom post type(properties) when saved equals the meta values in another custom post type(alerts)

I can get this work for normal match fields however I am trying to get it to work for a range for example, if ‘bedrooms’ in ‘properties’ is between ‘a_bedrooms_min’ and ‘a_bedrooms_max’ in ‘alerts’. My Query looks like this so far:

//Property Meta Fields
$meta_type = 'flat';
$meta_bedrooms = '2';

//Alert Meta Fields
a_bedrooms_min = 1
a_bedrooms_max = 3

SELECT ID
    FROM wp_posts
    INNER JOIN wp_postmeta m1
        ON ( wp_posts.ID = m1.post_id )    
    INNER JOIN wp_postmeta m2
        ON ( wp_posts.ID = m2.post_id )
    INNER JOIN wp_postmeta m3
        ON ( wp_posts.ID = m3.post_id )
WHERE
     wp_posts.post_type = 'alerts'
     AND wp_posts.post_status = 'publish'
     AND ( m1.meta_key = 'a_property_type' AND m1.meta_value = '$meta_type' )
     AND ( m2.meta_key = 'a_bedrooms_min' AND m2.meta_value >= '$meta_bedrooms' )
     AND ( m3.meta_key = 'a_bedrooms_max' AND m3.meta_value <= '$meta_bedrooms' )
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date
DESC;

The last two ANDs obviously will not work with what I am trying to do but how would can get this to work?

Thanks
Robert

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:35:34+00:00Added an answer on June 15, 2026 at 9:35 am

    I think you are close. you have a hybrid old ANSI vs more explicit JOIN. As you can see in this example, each one of the 3 criteria is joined to the meta table on all 3 parts explicitly. So, the ID should only appear ONCE per ID as each of the meta SHOULD AT MOST return one record, and the record will only qualify if all 3 meta find the respective match.

    SELECT 
          ID
       FROM 
          wp_posts
    
             INNER JOIN wp_postmeta m1
                ON wp_posts.ID = m1.post_id
               AND m1.meta_key = 'a_property_type'
               AND m1.meta_value = '$meta_type'
    
             INNER JOIN wp_postmeta m2
               on wp_posts.ID = m2.post_id
              AND m2.meta_key = 'a_bedrooms_min' 
              AND m2.meta_value <= '$meta_bedrooms'
    
             INNER JOIN wp_postmeta m3
               on wp_posts.ID = m3.post_id
              AND m3.meta_key = 'a_bedrooms_max' 
              AND m3.meta_value >= '$meta_bedrooms'
    
       WHERE
              wp_posts.post_type = 'alerts'
          AND wp_posts.post_status = 'publish'
    
       GROUP BY 
          wp_posts.ID
    
       ORDER BY 
          wp_posts.post_date DESC;
    

    I’ve revised as I believe the <= and >= were actually backwards. For example, your MINIMUM bedrooms from the meta_value was 1 and max is 3… you are LOOKING for 2 to be qualified.

    So, you NEED MIN_BEDROOMS <= 2 and the 2 <= MAX_BEDROOMS
    what was originally presented is MIN_BEDROOMS >= 2 (1 is never greater than 2)
    and MAX_BEDROOMS <= 2 (2 is never less than 2).

    Try again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write an UPDATE query that can check for a null
I am trying to write a query post such that it returns a list
I'm trying to write a query for the find-as-you-type search bar. What I want
Im trying to write an SQL query that will check in the table ‘persons’
I'm trying to write a query that will check today's date against my table
I'm trying to write a query like this, but just can't seem to work
I've been trying to write this query but can't seem to get it right
Im trying to write a query which will emit : the date/s which Overlapped
I'm trying to write a query that will return the closest match from a
I am trying to write a query and would like some help if possible.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.