query_postsで複数条件の指定
-
=から右をカンマ区切りで指定すれば出来ると思います。
query_posts(‘meta_key=test,test2,test3');自分もWordPressに触れて日が浅いため間違っていたらすみません
参考:WordPres Codex テンプレートタグ/query postsご回答ありがとうございます。
実は私もcatの複数指定のようにいかないかと思い、試してみたのですがうまくいきませんでした。
もしかしてmeta_keyには複数指定ができないのでしょうか?
その場合、複数条件を組み合わせてループを作成したい場合はどうしたら良いのでしょうか。
よろしくお願いします。未検証なので細かいことはよきにはからってください。
functions.php:function my_posts_where($where){ global $wpdb; return $where . " AND $wpdb->postmeta.meta_key IN ('test1','test2','test3') "; } function my_posts_join($join){ global $wpdb; return $join . " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) "; } function my_query_posts($q=''){ add_filter('posts_where', 'my_posts_where'); add_filter('posts_join', 'my_posts_join'); query_posts($q); remove_filter('posts_where', 'my_posts_where'); remove_filter('posts_join', 'my_posts_join'); }で query_posts() の代わりに my_query_posts() を使う。か、
global $wpdb; $posts = $wpdb->get_results(" SELECT $wpdb->posts.* FROM $wpdb->posts JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE $wpdb->postmeta.meta_key IN ('test1','test2','test3') ORDER BY $wpdb->posts.post_date DESC "); foreach($posts as $post) : /* do stuff */ endforeach;ご教授ありがとうございます。
ご指定いただいた方法にて複数指定を実現することができました。ただ、条件に一致したものがすべて表示されてしまうので、同じ記事がtest1,test2,test3に該当した場合に3回ほど表示されてしまいました。この重複を避けるにはどうしたら良いのでしょうか?
この点と、
もし仮にquery_posts(‘meta_key=test1&meta_value=東京’);
query_posts(‘meta_key=test2&meta_value=埼玉’);
query_posts(‘meta_key=test3&meta_value=神奈川’);というようなカスタムフィールドの”キーと値”の両方で複数判定(上記例の3つを一緒に条件にしたい)をしたい場合はどのようにしたら良いのでしょうか?
当初の質問を解決いただいたのに申し訳ありませんが、もしよろしければこれについてもご教授いただけると幸いです。
よろしくお願いします。これも未検証なのでよろしくです。
この重複を避けるには
functions.php に追加&変更:
function my_posts_distinct($distinct){ return 'DISTINCT'; } function my_query_posts($q=''){ add_filter('posts_distinct', 'my_posts_distinct'); add_filter('posts_where', 'my_posts_where'); add_filter('posts_join', 'my_posts_join'); query_posts($q); remove_filter('posts_distinct', 'my_posts_distinct'); remove_filter('posts_where', 'my_posts_where'); remove_filter('posts_join', 'my_posts_join'); }または
SELECT DISTINCT $wpdb->posts.*カスタムフィールドの”キーと値”の両方で複数判
functions.php 変更:
※全部一致の場合function my_posts_where($where){ global $wpdb; return $where . " AND $wpdb->postmeta.meta_key = 'test1' AND $wpdb->postmeta.meta_value = '東京' AND $wpdb->postmeta.meta_key = 'test2' AND $wpdb->postmeta.meta_value = '埼玉' AND $wpdb->postmeta.meta_key = 'test3' AND $wpdb->postmeta.meta_value = '神奈川' "; }※どれか一致の場合
function my_posts_where($where){ global $wpdb; return $where . " AND ( ( $wpdb->postmeta.meta_key = 'test1' AND $wpdb->postmeta.meta_value = '東京' ) OR ( $wpdb->postmeta.meta_key = 'test2' AND $wpdb->postmeta.meta_value = '埼玉' ) OR ( $wpdb->postmeta.meta_key = 'test3' AND $wpdb->postmeta.meta_value = '神奈川' ) ) "; }または
WHERE $wpdb->postmeta.meta_key IN ('test1','test2','test3')
を上記に倣って変更。素早いご回答を本当にありがとうございます。
重複の削除はできました。示していただきました、全部一致・部分一致ですが、
全部一致・部分一致共にそれぞれにAND $wpdb->postmeta.meta_key = ‘年’
AND $wpdb->postmeta.meta_value = ‘2010’
AND $wpdb->postmeta.meta_key = ‘月’
AND $wpdb->postmeta.meta_value = ‘3’
AND $wpdb->postmeta.meta_key = ‘日’
AND $wpdb->postmeta.meta_value = ‘1’AND (
( $wpdb->postmeta.meta_key = ‘年’
AND $wpdb->postmeta.meta_value = ‘2010’ )
OR
( $wpdb->postmeta.meta_key = ‘月’
AND $wpdb->postmeta.meta_value = ‘3’ )
OR
( $wpdb->postmeta.meta_key = ‘日’
AND $wpdb->postmeta.meta_value = ‘1’ )↑のような複数指定を試してみたのですが、うまくいきませんでした。
ここまでご教授いただいて申し訳ないのですが、もう一点ご教授願いたいのですが、
これで取得してきたデータの昇順・降順を指定するにはどのようにしたら良いのでしょうか?ご厚意に甘えた厚かましい質問だと思いますが、よろしくお願い致します。
Hello. I am from Russia, so I am righting in English.
I have a problem like this, which is not discussing in my country.My problem is that I need to compare 2 meta_keys in function
filter_where($where = '') {}for$my_query = new WP_QueryAs I understood it is exactly what you are talking about.
As I understood with help of Google Translate, that you have solved this problem,But when I tried to do the same, nothing showed UP, I think I have mistaken in parts of code.
Because it is difficult to understand what to copy.
Could you please write the whole code, where in query will be post with
meta_key = "test1" meta_value = "1" meta_key = "test2" meta_value = "2"and from 1 specific category CAT1_id.
Thank you for helping, hope for your answer. =)
Now my code is like this, works fine, but not quate accurate, I need to compare 2 meta_keys
function filter_where($where = '') { $date_start = ('30.03.2010'); $date_end = ('30.04.2012'); $where .= ' AND STR_TO_DATE(meta_value,"%d.%m.%Y") >= STR_TO_DATE("'.$date_start.'","%d.%m.%Y") AND STR_TO_DATE(meta_value,"%d.%m.%Y") <= STR_TO_DATE("'.$date_end.'","%d.%m.%Y") '; return $where; } function date_order($orderby = '') { $order .= 'STR_TO_DATE(meta_value,"%d.%m.%Y") ASC'; return $order; } add_filter('posts_where', 'filter_where'); add_filter('posts_orderby', 'date_order'); $args = array( 'category__and' => array($child_cat_id,$child_cat_id), 'meta_key' => $meta_field_name, 'showposts' => -1, ); $my_query = new WP_Query($args); remove_filter('posts_where', 'filter_where'); remove_filter('posts_orderby', 'date_order');Just in case =)
function filter_where($where = '') { global $wpdb; $date_start = ('30.03.2010'); $date_end = ('30.04.2012'); $where .= " AND STR_TO_DATE($wpdb->postmeta.meta_value,'%d.%m.%Y') >= STR_TO_DATE('$date_start','%d.%m.%Y') AND STR_TO_DATE($wpdb->postmeta.meta_value,'%d.%m.%Y') <= STR_TO_DATE('$date_end','%d.%m.%Y') "; return $where; } function date_order($orderby = '') { global $wpdb; $orderby = "STR_TO_DATE($wpdb->postmeta.meta_value,'%d.%m.%Y') ASC"; return $orderby; }I think I got it. Thank you, will try soon.
Hello, friends. I have tried to use one of codes from your discussing, and I have a problem with it.
I do not understand, does this code should work correct?
function my_query_posts($q=''){ add_filter('posts_where', 'my_posts_where'); add_filter('posts_join', 'my_posts_join'); query_posts($q); remove_filter('posts_where', 'my_posts_where'); remove_filter('posts_join', 'my_posts_join'); } function my_posts_where($where){ global $wpdb; return $where . " AND $wpdb->postmeta.meta_key = 'test1' AND $wpdb->postmeta.meta_value = 'value1' AND $wpdb->postmeta.meta_key = 'test2' AND $wpdb->postmeta.meta_value = 'value2' AND $wpdb->postmeta.meta_key = 'test3' AND $wpdb->postmeta.meta_value = 'value3' "; } function my_posts_join($join){ global $wpdb; return $join . " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) "; }I am asking, because it does not show anything, dispite of I have a post with theese fields and values.
but I want to add, that filter
function my_posts_where($where){ global $wpdb; return $where . " AND $wpdb->postmeta.meta_key = 'test1' AND $wpdb->postmeta.meta_value = 'value1' "; }Works fine. Please, can you wright me (in english) – did you solve this problem, and right the correct code. 感謝 =)
トピック「query_postsで複数条件の指定」には新たに返信することはできません。