admin管理员组

文章数量:1122832

WHAT I'M TRYING TO DO

I'm using woocommerce, but the question could work for WP categories also. When searching for a product, the search engine only looks for products info (title and description) and display them. I'm looking for a way to include product categories (product_cat) title and description in the query so I can list all the products from the found categories.

WHAT I DON'T WANT TO DO

I don't want to display the categories, only the products in these categories if the terms are found in the title or description of these very categories. ex: not that

TL;DR

Let's say I have a category named "pet" with products named cat, dog, etc.. but without the term "pet" in their title nor description. I would like the user to be able to look for pet and see all the products from this "pet" category in the search results.

So far I tried this

function SearchFilter($query) {
  if ($query->is_search && !is_admin()) {
    // Get the search query
    $search_query = $query->query_vars['s'];

    
    $query->set( 'tax_query', array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'name',
            'terms'    => $search_query,
            'operator' => 'LIKE'
        ),
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'description', // <-- I found out that this does not work. see: 
            'terms'    => $search_query,
            'operator' => 'LIKE'
        )
    ));

    $query->set('post_type', array('product'));
    $query->set( 'wc_query', 'product_query' );


    if (isset( $_GET['showall'] ) && $_GET['showall'] == 1 ) {
      $query->set( 'paged', 1 );
      $query->set( 'posts_per_page', - 1 );
    } else {
      $query->set( 'posts_per_page', 12 );
    }
  
  }
  return $query;
}
add_filter('pre_get_posts','SearchFilter');

But it doesn't seem to work. From what I found, my guess would be to use posts_join, posts_where and post_district filters to search in the product_cat first, save the IDs and then query the products with these taxonomies (making sure we also display the products with the terms in their title/description). But I'm a bit lost with this.

WHAT I'M TRYING TO DO

I'm using woocommerce, but the question could work for WP categories also. When searching for a product, the search engine only looks for products info (title and description) and display them. I'm looking for a way to include product categories (product_cat) title and description in the query so I can list all the products from the found categories.

WHAT I DON'T WANT TO DO

I don't want to display the categories, only the products in these categories if the terms are found in the title or description of these very categories. ex: not that

TL;DR

Let's say I have a category named "pet" with products named cat, dog, etc.. but without the term "pet" in their title nor description. I would like the user to be able to look for pet and see all the products from this "pet" category in the search results.

So far I tried this

function SearchFilter($query) {
  if ($query->is_search && !is_admin()) {
    // Get the search query
    $search_query = $query->query_vars['s'];

    
    $query->set( 'tax_query', array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'name',
            'terms'    => $search_query,
            'operator' => 'LIKE'
        ),
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'description', // <-- I found out that this does not work. see: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
            'terms'    => $search_query,
            'operator' => 'LIKE'
        )
    ));

    $query->set('post_type', array('product'));
    $query->set( 'wc_query', 'product_query' );


    if (isset( $_GET['showall'] ) && $_GET['showall'] == 1 ) {
      $query->set( 'paged', 1 );
      $query->set( 'posts_per_page', - 1 );
    } else {
      $query->set( 'posts_per_page', 12 );
    }
  
  }
  return $query;
}
add_filter('pre_get_posts','SearchFilter');

But it doesn't seem to work. From what I found, my guess would be to use posts_join, posts_where and post_district filters to search in the product_cat first, save the IDs and then query the products with these taxonomies (making sure we also display the products with the terms in their title/description). But I'm a bit lost with this.

Share Improve this question asked Apr 9, 2024 at 8:23 allumetteallumette 12 bronze badges 2
  • Not sure if "LIKE" exists for tax_query, Try removing : 'operator' => 'LIKE' – mysticalghoul Commented Apr 9, 2024 at 9:41
  • 1 You're right. But that wasn't a good start anyway. Found out a solution. I posted it – allumette Commented Apr 9, 2024 at 13:19
Add a comment  | 

1 Answer 1

Reset to default 0

Found out my solution by filtering the search query. Hope that helps:

function new_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {
        $join .= "
        INNER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id
        INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
        INNER JOIN {$wpdb->terms} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id";
    }

    return $join;
}
add_filter('posts_join', 'new_search_join' );


function new_search_where( $where ) {
    global $pagenow, $wpdb;

    if ( is_search()) {


        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "
              (".$wpdb->posts.".post_title LIKE $1)
              OR
              (".$wpdb->posts.".post_content LIKE $1)
              OR
              (".$wpdb->terms.".name LIKE $1)
              OR
              (".$wpdb->term_taxonomy.".description LIKE $1)
            ", $where );

    }

    return $where;
}
add_filter( 'posts_where', 'new_search_where' );

/**  Prevent duplicates  */
function new_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( 'posts_distinct', 'new_search_distinct' );

本文标签: phpIncluding product categories (productcat taxonomy) title and description in search query