php - 在查询中加入第二个表不会在 WordPress 中返回任何内容

标签 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 中返回任何内容。它向我显示“没有结果”

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

最佳答案

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

关于php - 在查询中加入第二个表不会在 WordPress 中返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38835750/

相关文章:

php - Joomla:为什么 "Extension Manager"菜单项没有显示在 "Extensions"下拉菜单中?

php - 管理循环中的多个 SQL 语句

mysql - 查询中存在多个 LIKE 语句

mysql - 在具有相同 ID 的列上更新表值(设置最小值)

php - 在 Wordpress 中将变量 php 传递给 css 的正确方法

php - 每月自动重置的引用编号

PHP MySQL 随机

mysql - DESCRIBE命令的内部定义是什么以及如何扩展?

php - WordPress:<主体类 ="customize-support">

wordpress - 是否可以让 Firebase 托管您的静态网站,而另一个提供商在同一域下托管您的博客?