1

I am working on search. I am trying to search the product by tag or category. I tried below code but it's not working. I am getting

Nothing Found

Also title and content is also not working.

Would you help me out in this?

<?php
/**
 * The template for displaying search results pages
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
 *
 * @package CoolPickings
 */

get_header();
?>

    <section id="primary" class="content-area mainSearch">
        <main id="main" class="site-main">
            <div class="equalPadding">
                <div class="cp-seeWrapper">
        <?php 

        $getSearch=get_search_query();

    $args = array(
    'tax_query' => array(
       'relation' => 'OR',
        array(
            'taxonomy' => 'product_tag',
            'field' => 'slug',
            'terms' => $getSearch
        ),
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => $getSearch
        )
    ),
    'post_type' => 'product'
);
$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ):
                    get_search_form();//search form 
                    ?>
                    <div class="resultCount">
                    <?php global $wp_query;echo $wp_query->found_posts.' RESULTS';?>
                    </div>
           <div class="row">
            <?php   

    while ( $the_query->have_posts() ) {
           $the_query->the_post();?>
         <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12 ">
                <div class="cp-shadow cp-seeSinglePostWrapper">
                <?php the_post_thumbnail();?>
                    <div class="bg-white single-post-box">
                    <div class="d-flex cp-CategoryList">
                     <div class="seeDate"><?php echo get_the_date('F j, Y'); ?></div>
                        <div class="cp_cat_list">
                            <?php $cat = get_the_category();
                            ?>

                            <a href="<?php echo esc_url( get_category_link( $cat[0]->term_id ) );?>"><?php echo $cat[0]->cat_name?></a>
                        </div>
                    </div>
                        <div class="cp-b-content"><h2><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>"><?php echo wp_trim_words(get_the_title(), 12, '...'); ?></a></h2></div>
                        <p><?php echo wp_trim_words(get_the_excerpt(), 25, '...'); ?></p>
                </div>
                </div>
                </div>
             <?php }?>

            <?php 

        else :

            get_template_part( 'template-parts/content', 'none' );

        endif;
        ?>
                    </div>
        </div>
        </div>
        </main><!-- #main -->
    </section><!-- #primary -->

<?php
get_sidebar();
get_footer();
0

Try setting terms as array:

'terms' => array($getSearch)
0

Finally, I found my answer. I don't know this is the best way or know but it's solved my issue.

I tried below code in search.php

$getSearch = get_search_query();
$query = new WP_Query([
  'post_type' => 'any',
  'tax_query' => array(
    'relation' => 'OR',
    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();
  } ?>

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.