R
R
Ruslan2013-08-08 05:06:11
PHP
Ruslan, 2013-08-08 05:06:11

Query from the database, one field - many values?

Actually in WordPress, data is stored in add. fields such as meta_key='type', meta_value='buy', meta_key='price', meta_value='10000'etc. task is to make a selection. there are a lot of these metafields, WP tools can be attached to the search_meta_jointype:

$join .= " JOIN  $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";

and in the same way to search_meta_where, and here I don’t know how to do it right, I get such a request
SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts JOIN  wp_postmeta ON wp_posts.ID = wp_postmeta.post_id  WHERE 1=1  AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') AND ( wp_postmeta.meta_value IN ('rent','flat')) GROUP BY wp_posts.ID HAVING COUNT(*) = 2

that is, I need records that are both rent and flat, am I doing it right (there, in addition to rent and flat, there is still, but I shortened it)? But I also need to search for the price between two values, the area between two values, etc., how to make such a query correctly?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Artyom Karetnikov, 2013-08-08
@art_karetnikov

Colleague, you are using WordPress and metafields for this in vain - there are fields of the varchar type and without indexes. After a while, it will start to slow down terribly for you. Design your structure the right way - with primary keys like int and so on.

R
Ruslan, 2013-08-08
@rOOse

It seems he figured it out himself, for each key-value you will need to make a new JOIN of type

JOIN wp_postmeta as wp_postmeta_1 ON wp_posts.ID = wp_postmeta_1.post_id

and further
AND wp_postmeta_1.meta_value > 8000

I
Igor, 2013-08-08
@igoravr

Did using the WP_Query class not work for you? There are Custom Field Parameters with which you can make selections according to the necessary conditions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question