3

I've got a WooCommerce category for sold items within WooCommerce's custom post type of 'product'. What I'm trying to do is have the site search only return results from the product post type but also exclude the sold items category. Any code I seem to find to exclude categories from search doesn't seem to work but I'm guessing that's because it's a category within a CPT.

The code I've got for filtering searches down to just to the product CPT (in functions.php):

function mySearchFilter($query) {
    $post_type = $_GET['type'];
    if (!$post_type) {
        $post_type = 'product';
    }
    if ($query->is_search) {
        $query->set('post_type', $post_type);
    };
    return $query;
};

And the code I've tried to exclude categories:

function remove_categories_wp_search($query) {
if ($query->is_search) {
    $query->set('cat','-247');
}
    return $query;
}
add_filter('pre_get_posts','remove_categories_wp_search');

Any ideas?

EDIT: As per ialocin's suggestion I also tried:

function wpse188669_pre_get_posts( $query ) {
    if ( 
        ! is_admin() 
        && $query->is_main_query() 
        && $query->is_search() 
    ) {
        // set your parameters according to
        // https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
        $tax_query = array(
            // likely what you are after
            'taxonomy' => 'sold-gallery',
            'operator' => 'NOT IN',
        );
        $query->set( 'tax_query', $tax_query );
  }
}
add_action( 'pre_get_posts', 'wpse188669_pre_get_posts' );
4

The problem with your approach is that woocommerces product category is a custom taxonomy called product_cat. But with cat you are addressing the built-in category. Taxonomies can be addressed with a tax query, simplified example below:

function wpse188669_pre_get_posts( $query ) {
    if ( 
        ! is_admin() 
        && $query->is_main_query() 
        && $query->is_search() 
    ) {
        $query->set( 'post_type', array( 'product' ) );
        // set your parameters according to
        // https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
        $tax_query = array(
            array(
                // likely what you are after
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => 'sold-gallery',
                'operator' => 'NOT IN',
            ),
        );
        $query->set( 'tax_query', $tax_query );
  }
}
add_action( 'pre_get_posts', 'wpse188669_pre_get_posts' );
  • That makes so much sense. However, the code you provided didn't seem to work although I'm not sure if that's intentional (you mentioned simplified). I've edited the OP with what I tried as well. – lotech May 19 '15 at 18:23
  • It couldn't work, I wanted to add a parameter for you, in a hurry, which never is a good idea, so it was plain wrong. What you did is better, but not completely correct, see the code in my answer for what I think you actually have to do, just a confusion with taxonomy and terms I guess. @user1001657 – Nicolai May 19 '15 at 18:37
  • 1
    Your tax_query is wrong, should be an array of an array :-) – Pieter Goosen May 19 '15 at 18:44
  • Thank you both, gents :) I'll post the final answer separately. – lotech May 19 '15 at 18:50
  • 1
    Ahh, damn, thanks a lot Mr. Goosen :) @PieterGoosen – Nicolai May 19 '15 at 19:06
1

iolocin, I took your edited answer and as Pieter Goosen mentioned, made sure it was an array in an array and as per the codex. Final working code:

function wpse188669_pre_get_posts( $query ) {
    if ( 
        ! is_admin() 
        && $query->is_main_query() 
        && $query->is_search() 
    ) {
        $tax_query = array(
            'post_type' => 'product',
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',
                    'terms' => 'sold-gallery',
                    'operator' => 'NOT IN',
                ),
            ),
        );
        $query->set( 'tax_query', $tax_query );
  }
}
add_action( 'pre_get_posts', 'wpse188669_pre_get_posts' );
  • Are sure about that, because $tax_query contains just that, namely the tax query. Yours looks like written for a WP_Query call. – Nicolai May 19 '15 at 19:12
  • Strange but it was definitely not working before I reformatted as above and now it seems to be doing the trick. – lotech May 19 '15 at 20:17
  • Just checked it out again and your answer is definitely working now. Not sure if you edited it after I tried it or if I was just being a little silly but all fixed either way. I'll mark your answer as accepted. Thanks for the time! – lotech May 19 '15 at 20:18
  • My pleasure! I think edited it afterwards, but maybe just for formatting reasons, but whatever...it works now :) – Nicolai May 19 '15 at 20:21

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.