0

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;
}
  • Normally WP_Query searches in the title, the content and the excerpt of the post (@see WP_Query::parse_search()). Can you check if there is any plugin interfering with the search? There is also the posts_search filter, which is applied to the search part of the query string. – JHoffmann Nov 26 '16 at 11:42
  • Because your search argument 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 your posts_where method to reimplement the argument? – TheDeadMedic Nov 26 '16 at 12:13
  • Looks like there is a method in the same class that is called posts_where. Could you paste this method into your question please to help us? – Dan. Nov 26 '16 at 16:02
1

WP_Query's s parameter in it's arguments IS used to search the post_content too.

There must be something that is altering this behaviour.

Looks like there's a method inside your class that is altering the query: posts_where.

Or maybe it's that you've put the s argument into an array of it's own, rather than being part of the $args array itself.

The posts_where filter allows you to edit the SQL search query, before it is sent. Removing that code shouldn't 'break' your code, but will probably produce different results.

  • Yes bro You r right I found 'posts_where' method in the same file itself. I am updating the code above. can u please check it once. – Prasad Patel Nov 27 '16 at 5:39
  • Worked like a charm. I just commented the 'add_filter' method. could you please briefly tell me what is the importance of 'add_filter' method? – Prasad Patel Nov 27 '16 at 6:08
  • Answer edited last paragraph – Dan. Nov 27 '16 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.