2016-08-08 7 views
0

ページ上にデフォルトのwpフィールドの検索フィールドがあります。検索ボタンを押すと、postsテーブルから正しく検索され、すべての結果が表示されます。質問で2番目のテーブルに参加してもワードプレスで何も返されません

ここで、検索する場所から2番目のテーブルに参加して、そこに結果があれば表示したいと思います。私はこれを追加すると、それがポストからもhdflvvideoshareからでもない、何も返さない

function vh_search_meta_data_join($join) { 
    global $wpdb; 

    // Only join the post meta table if we are performing a search 
    if (get_query_var('s') == '') { 
     return $join; 
    } 

    // Only join the post meta table if we are on the Contacts Custom Post Type 
    if (!in_array('videogallery', get_query_var('post_type'))) { 
     return $join; 
    } 

    // Join the post meta table 
    $join .= " LEFT JOIN ".$wpdb->prefix."hdflvvideoshare"; 

    return $join; 
} 

function vh_search_meta_data_where($where) { 
    global $wpdb; 

    // Only join the post meta table if we are performing a search 
    if (get_query_var('s') == '') { 
     return $where; 
    } 

    // Only join the post meta table if we are on the Contacts Custom Post Type 
    if (!in_array('videogallery', get_query_var('post_type'))) { 
     return $where; 
    } 

    // Get the start of the query, which is ' AND ((', and the rest of the query 
    $startOfQuery = substr($where, 0, 7); 
    $restOfQuery = substr($where ,7); 

    // Inject our WHERE clause in between the start of the query and the rest of the query 
    $where = $startOfQuery . "(" . $wpdb->prefix."hdflvvideoshare.description LIKE '%" . get_query_var('s') . "%') OR " . $restOfQuery ." GROUP BY " . $wpdb->posts . ".ID"; 

    // Return revised WHERE clause 
    var_dump($where); 
    return $where; 
} 

:私はこれを追加したテーマフォルダ内のファイルfunction.phpでそう

。それは私に"No results"を示しています。

var_dump($where);リターンこれは:

string(625) " AND (((wpdu_hdflvvideoshare.description LIKE '%driver%') OR (((wpdu_posts.post_title LIKE '%driver%') OR (wpdu_posts.post_content LIKE '%driver%'))) OR ((tter.name LIKE '%driver%')) OR ((tter.slug LIKE '%driver%')) OR ((ttax.description LIKE '%driver%')) OR ((m.meta_value LIKE '%driver%')) OR ((wpdu_posts.post_excerpt LIKE '%driver%')) OR (((cmt.comment_content LIKE '%driver%')) AND cmt.comment_approved = '1') OR ((u.display_name LIKE '%driver%')))) AND (wpdu_posts.post_password = '') AND wpdu_posts.post_type IN ('page', 'post', 'videogallery') AND (wpdu_posts.post_status = 'publish') GROUP BY wpdu_posts.ID" string(541) "(((wpdu_posts.post_title LIKE '%driver%') OR (wpdu_posts.post_content LIKE '%driver%'))) OR ((tter.name LIKE '%driver%')) OR ((tter.slug LIKE '%driver%')) OR ((ttax.description LIKE '%driver%')) OR ((m.meta_value LIKE '%driver%')) OR ((wpdu_posts.post_excerpt LIKE '%driver%')) OR (((cmt.comment_content LIKE '%driver%')) AND cmt.comment_approved = '1') OR ((u.display_name LIKE '%driver%')))) AND (wpdu_posts.post_password = '') AND wpdu_posts.post_type IN ('page', 'post', 'videogallery') AND (wpdu_posts.post_status = 'publish')" 

何が問題です。なぜ私はこれを動作させることができないのですか?

アップデート:私はこれがあなたのクエリがWHERE句内部GROUP BY条件を持っている

SELECT DISTINCT SQL_CALC_FOUND_ROWS wpdu_posts.* FROM wpdu_posts LEFT JOIN wpdu_hdflvvideoshare LEFT JOIN wpdu_term_relationships AS trel ON (wpdu_posts.ID = trel.object_id) LEFT JOIN wpdu_term_taxonomy AS ttax ON ((ttax.taxonomy = 'category' OR ttax.taxonomy = 'post_tag' OR ttax.taxonomy = 'post_format' OR ttax.taxonomy = 'bp_member_type' OR ttax.taxonomy = 'bp-email-type') AND trel.term_taxonomy_id = ttax.term_taxonomy_id) LEFT JOIN wpdu_terms AS tter ON (ttax.term_id = tter.term_id) LEFT JOIN wpdu_comments AS cmt ON (cmt.comment_post_ID = wpdu_posts.ID) LEFT JOIN wpdu_postmeta AS m ON (wpdu_posts.ID = m.post_id) LEFT JOIN wpdu_users AS u ON (wpdu_posts.post_author = u.ID) WHERE 1=1 AND (((((wpdu_hdflvvideoshare.description LIKE '%driver%') OR (((wpdu_posts.post_title LIKE '%driver%') OR (wpdu_posts.post_content LIKE '%driver%'))) OR ((tter.name LIKE '%driver%')) OR ((tter.slug LIKE '%driver%')) OR ((ttax.description LIKE '%driver%')) OR ((m.meta_value LIKE '%driver%')) OR ((wpdu_posts.post_excerpt LIKE '%driver%')) OR (((cmt.comment_content LIKE '%driver%')) AND cmt.comment_approved = '1') OR ((u.display_name LIKE '%driver%')))) AND (wpdu_posts.post_password = '') AND wpdu_posts.post_type IN ('page', 'post', 'videogallery') AND (wpdu_posts.post_status = 'publish' OR wpdu_posts.post_type = 'attachment' OR wpdu_posts.post_status = 'draft') GROUP BY wpdu_posts.ID) AND post_type != 'revision') AND post_status != 'future' ORDER BY wpdu_posts.post_title LIKE '%driver%' DESC, wpdu_posts.post_date DESC LIMIT 0, 10 
+0

完全SQLコマンドとは何ですか?あなたのvar_dumpは完全なSQL文ではありません –

+0

どのように完全なクエリを見ることができますか? –

+0

var_dump SQL文全体を含む変数。 –

答えて

2

生成された完全なクエリであると信じています。他にも問題があるかもしれませんが、確かに構文エラーが発生することがあります。

関連する問題