I have search field on the page which is default wp field I think. When I hit search button it is searching properly from the posts
table and show all results.
Now I want to join second table from where to search also and show results from there if there is any.
So in the function.php
file in the theme folder I have added this:
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;
}
When I add this it doesn't return anything neither from posts nor from hdflvvideoshare. It's showing me "No results"
.
var_dump($where);
return this:
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')"
What is wrong. Why I can't get this to work?
Update: I believe that this is the full query which is generated
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
Your query has a GROUP BY
condition inside the WHERE
clause. There may be other issues, but that will certainly ensure that you get a syntax error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.