2016-01-04 46 views
0

我有2元密鑰KEY1和KEY2,我要尋找一個查詢這裏使用兩個關鍵 是我的PHP查詢如何使用FIND_IN_SET在wp_query

$where = "keyone = 1 or FIND_IN_SET($id, `keytwo`) <>0"; 

其不過我是工作的罰款與MySQL試圖將這個概念轉換成wp_query,但我沒有得到任何結果。

refrence guide from someone

這段代碼也沒有提前上班

感謝。

+0

參見[這個類似的帖子(http://wordpress.stackexchange.com/a/18715/12496)關於使用自定義'LIKE'查詢。基本上你只需要過濾查詢的WHERE子句並注入你自己的SQL字符串。 –

+0

這對我不起作用,我必須在元鍵而不是標題中找到它。 – btechguy

回答

0

我發佈的鏈接只是爲您注入自己的自定義WHERE條款,這可能是您唯一的選擇。如果你看看source for WP_Meta_Query,你會發現比較運算符是硬編碼的。因爲WordPress從來沒有真正希望存儲SET的值,所以對這些值也沒有任何本地支持。我會嘗試這樣的事:

//Function to inject our custom WHERE clauses 
function so_34594266_post_where($where, &$wp_query) 
{ 
    //Somehow this function needs to be made "aware" of the $id variable, 
    //either a class-level variable, global (yuck) or some function call 
    if($wp_query->is_main_query()) 
    { 
     $where = "keyone = 1 or FIND_IN_SET($id, `keytwo`) <>0"; 
    } 

    return $where; 
} 

//Tell WordPress to use our function 
add_filter('post_where', 'so_34594266_post_where'); 

//Get our posts (this could also be a WP_Query object or whatever else) 
$posts = get_posts('...your stuff here...'); 

//Tell WordPress to stop using our function 
remove_filter('post_where', 'so_34594266_post_where');