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

  • SEARCH
  • Home
  • 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 3306856
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:20:17+00:00 2026-05-17T21:20:17+00:00

I’ve got this query so far: if(!empty($_SESSION[‘s_property_region’])) { $sqlWHERE .= AND $wpdb->postmeta.meta_key = ‘property_region’

  • 0

I’ve got this query so far:


if(!empty($_SESSION['s_property_region'])) {
    $sqlWHERE .= " AND $wpdb->postmeta.meta_key = 'property_region' AND $wpdb->postmeta.meta_value = '".$_SESSION['s_property_region']."'";
}
if(!empty($_SESSION['s_property_bedrooms'])) {
    $sqlWHERE .= " AND $wpdb->postmeta.meta_key = 'property_bedrooms' AND $wpdb->postmeta.meta_value = '".$_SESSION['s_property_bedrooms']."'";
}
$sql    =   "SELECT $wpdb->posts.ID
             FROM $wpdb->posts
             LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
             WHERE $wpdb->posts.post_status = 'publish'
             AND $wpdb->posts.post_type = 'property'
             ".$sqlWHERE."
             ORDER BY $wpdb->posts.post_date DESC";

It works if I just search for one custom field but not the two together. I know why and its because the way the LEFT Join works. There isnt going to be a row where post_type can be two things at once.

after the join Im going to get a structure like this:

|   ID   |   meta_key          |   meta_value   |
|   1    |   property_region   |   East         |
|   1    |   property_bedrooms |   4            |
|   2    |   property_region   |   West         |
|   2    |   property_bedrooms |   2            |

is there a query that can turn the two tables into a structure like this:

|   ID   |   property_region   |   property_bedrooms   |
|   1    |   East              |   4                   |
|   2    |   West              |   2                   |

If the query can make the table look like above then my simple WHERE this=that AND this=that will work.

Any help on this matter is greatly appreciated

Thanks

Scott

EDIT: After some searching about I now put this code together: http://pastebin.com/5YYDLyeR It works and returns the structure I’m after but as soon as I try to do a WHERE on property_region and property_bedrooms I get: #1054 – Unknown column ‘property_bedrooms’ in ‘where clause’

EDIT2: Ok I have taken PMV code and to be able to do my WHERE clause I’ve had to do a sub-query: http://pastebin.com/dvTgsU7S This code does what I want but are there any better ways of achieving this query? I presume performance will be an issue when I have 1000+ entries in the DB…

  • 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-05-17T21:20:18+00:00Added an answer on May 17, 2026 at 9:20 pm

    Feed your entire query into something like this:

    SELECT ID
    ,      MAX(CASE WHEN meta_key = 'property_region' THEN meta_value END) AS property_region
    ,      MAX(CASE WHEN meta_key = 'property_bedrooms' THEN meta_value END) AS property_bedrooms
    FROM (your query)
    GROUP BY ID
    HAVING MAX(CASE WHEN meta_key = 'property_region' THEN meta_value END) = 'east'
    AND    MAX(CASE WHEN meta_key = 'property_bedrooms' THEN meta_value END) = 4
    

    EDIT: The HAVING clause. It’s ugly, but I think it’s what you’re looking for. You’ll still have to build it in the php code, but I think it should give you what you want.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.