1

I am working on search. I am able to search post by tag and category but not able to search by title and content.

I tried below code but it's not working. Would you help me out?

$getSearch = get_search_query();
//print($search);
$query = new WP_Query([
  'post_type' => 'post',
  'tax_query' => array(
    'relation' => 'OR',
       array(
      's'=> $getSearch // for title and content
    ),
    array(
      'taxonomy' => 'post_tag', //for tag
      'field' => 'slug',
      'terms' => array($getSearch),
    ),
    array(
      'taxonomy' => 'category', // for category
      'field' => 'slug',
      'terms' => array($getSearch),
    ),
  ),
]); 

if ($query->have_posts() ):
      while ( $query->have_posts() ) {$query->the_post();?>
<!--some content-->
<?php }
else :
       get_template_part( 'template-parts/content', 'none' );
        endif;
?>

?>

function.php

add_action( 'pre_get_posts', 'my_search_exclude' );
function my_search_exclude( $query ) {
  if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
    $query->set( 'post_type', array( 'post', 'page', 'product' ) );
    $query->set( 'post__not_in', array( 471 ) );
     $taxquery=  array(
  'post_type' => 'any',
  's'=> $query->get('s')
);
$query->set( 'tax_query', $taxquery);

  }
}
7
  • Hello! i see your tax_query has a post_type and an s value, this is extremely unusual and is not what the docs say goes in a tax_query. The tax_query parameter only has taxonomy related items in it, why have you put the s and post_type parameters inside it? The docs never say to do this, and have examples that say otherwise – Tom J Nowell Dec 15 '19 at 12:05
  • @TomJNowell, I thought 's' is use for thr title and content search so I used. I am still confused what code I have to use fir title and content – Naren Verma Dec 15 '19 at 18:08
  • s is just a generic search term parameter for the built in WP search, it's not a reusable generic field, you can't search taxonomy terms that way, the s parameter only goes in a single place. See the official doc on WP_Query for how to use the parameters developer.wordpress.org/reference/classes/wp_query/…, you can't just add an s parameter to a tax_query to search tags/categories/etc, same goes for meta_query, it's much more involved than that. If it's just search the post title and content, then use the s parameter as documented – Tom J Nowell Dec 15 '19 at 18:10
  • @TomJNowell, Yes, I am checking this link from last 3 days but still I am trying to solve this. – Naren Verma Dec 15 '19 at 18:14
  • I used the above query because I have to search the product by tag and category. Now tag and category working but title and content not working – Naren Verma Dec 15 '19 at 18:15
0

Edit: It appears your actual problem is searching tags and categories, hence placing the s parameter inside the tax_query.

There is no way to search tags categories and other terms using only the fields provided by WP_Query. Your query doesn't work because what you want isn't possible with just the parameters available.

Instead of asking how to fix your solution/attempt, I'd recommend asking a new question for how to search taxonomy terms/categories/tags


Original answer:

The reason your search doesn't work, is because your query has no search:

$query = new WP_Query([
  'post_type' => 'post',
  'tax_query' => array(
    'relation' => 'OR',
       array(
      's'=> $getSearch // for title and content
    ),
...

For some reason, the s parameter has been inserted inside the tax_query, which doesn't make a lot of sense.

For example, here is a normal query that searches:

$query = new WP_Query([
  's' => 'search terms',
etc..

But for some reason you've put it inside the tax_query:

$query = new WP_Query([
  'tax_query' => array(
    'relation' => 'OR',
       array(
          's'=> $getSearch // for title and content
       ),

This will not work, and is not what the documentation and examples suggest.

What's more, the code is creating a brand new query unnecessarily, which also doesn't make sense as it doubles the page loading time, and breaks pagination.

Instead, all these issues can be bypassed trivially by using the pre_get_posts filter, e.g. in functions.php:

add_action( 'pre_get_posts', function( \WP_Query $q ) {
    if ( !$q->is_main_query() || !$q->is_search() ) {
        return;
    }
    $q->set( 'tax_query', ....... );
} );

Now you can use search.php as intended, with a standard post loop, and everything would work as it normally does. No need for a query at the top of the file.

The TLDR: If you want to change the posts WP shows, tell WP what you want via pre_get_posts. Don't create a second query, it cripples performance and introduces lots of new problems

14
  • Sorry for the late reply, @Tom, I added function.php search code. I need to know what code I have to use inside $q->set( 'tax_query', ....... ); ? – Naren Verma Dec 13 '19 at 3:25
  • @NarenVerma the second parameter is the same as what you would put in the arguments array. I won't update my answer with a copy pasta but beginner level PHP should be enough to figure out what goes in there ( it should be a simple copy paste operation ). Also, none of this will work as long as you still have the WP_Query loop in your template, it needs to be changed for a standard post loop – Tom J Nowell Dec 15 '19 at 12:03
  • Trust me I haven't understood your solution so I updated my code in the question. I am confused what code I have to use inside function – Naren Verma Dec 15 '19 at 18:03
  • @NarenVerma I suspect you don't have a basic grasp of PHP? Note that I won't be adding a copy paste solution to my answer, a beginner level understanding of PHP is necessary to understand any answers you get here – Tom J Nowell Dec 15 '19 at 18:09
  • I need to know the answer so that i will understand where I am doing the mistake. I am trying to learn PHP and wordpress. – Naren Verma Dec 15 '19 at 18:17

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.