I have written a search query for my custom post. I am using search parameter of WP_Query for this then it has to search for both 'post_title' & 'post_content' but it is searching only for 'post_title' and not searching the 'post_content'. below is my code :
$institute_name = get_user_meta($user_ID, 'inistitute_name', true);
//add_filter( 'posts_where' , array($this,'posts_where' )); this line Irritated me.
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
'post_type' => 'mypost',
array( 's' => $keyword ),
'meta_query' => array (
array (
'key' => 'inistitute_name',
'value' => array ($institute_name),
'compare' => 'IN'
)
),
'posts_per_page' => -1,
'paged' => $paged,
'post_status' => 'publish',
);
$the_query = new WP_Query($args);
//remove_filter( 'posts_where', array($this,'posts_where' ));
echo "<pre>";
print_r($the_query);
echo "</pre>";
If I print that result then I am getting the query like below:
SELECT wp_dxwe_posts.* FROM wp_dxwe_posts INNER JOIN wp_dxwe_postmeta ON
( wp_dxwe_posts.ID = wp_dxwe_postmeta.post_id ) WHERE 1=1 AND
( (wp_dxwe_postmeta.meta_key = 'inistitute_name' AND
CAST(wp_dxwe_postmeta.meta_value AS CHAR) IN ('ITeLearn') ))
AND wp_dxwe_posts.post_type = 'mypost' AND ((wp_dxwe_posts.post_status = 'publish')) AND
post_title LIKE '%launch%' GROUP BY wp_dxwe_posts.ID
ORDER BY wp_dxwe_posts.post_date DESC
As I said it is working fine for only 'post_title' but sometimes not working, it is not always working. Can anyone please tell me what's wrong in this code? Thanks in advance.
Update: below is the 'post_where' method I found in the same file. I think Either its better to remove this method or don't call the 'posts_where' method by 'add_filter' method. Finally, I got the solution by commenting the 'add_filter' & 'remove_filter' code lines. But could u please tell me bit briefly about 'add_filter' method? will I get any problem as I commented the 'add_filter' method.? I am Updating the above code.
function posts_where( $where ) {
if(isset($_GET['tewa_search'])) {
$var=$_GET['tewa_search'];
$where .= " AND post_title LIKE '%".$var."%'";
}
return $where;
}
posts_search
filter, which is applied to the search part of the query string.s
should be an immediate index of$args
, whereas you've put it in a nested array - I can't see how it works at all at the moment, unless you're doing something in yourposts_where
method to reimplement the argument?posts_where
. Could you paste this method into your question please to help us?