在查询中加入第二个表并不会在wordpress

时间:2016-08-08 18:04:56

标签: php mysql wordpress

我认为页面上的搜索字段是默认的wp字段。当我点击搜索按钮时,它会从posts表格中正确搜索并显示所有结果。

现在我想加入第二个表,从哪里搜索并显示结果,如果有的话。

所以在主题文件夹的function.php文件中我添加了这个:

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;
}

当我添加它时,它既不会从帖子也不会从hdflvvideoshare返回任何内容。它告诉我"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')"

出了什么问题。为什么我无法让这个工作?

更新:我认为这是生成的完整查询

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

1 个答案:

答案 0 :(得分:2)

您的查询在GROUP BY子句中有WHERE条件。可能还有其他问题,但这肯定会确保您收到语法错误。