Warning: include_once(/home4/aquapan/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache-phase1.php): failed to open stream: No such file or directory in /home/revela25/public_html/cjbarnaby/wp-content/advanced-cache.php on line 22

Warning: include_once(): Failed opening '/home4/aquapan/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache-phase1.php' for inclusion (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/revela25/public_html/cjbarnaby/wp-content/advanced-cache.php on line 22

Warning: include(/home4/aquapan/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache-base.php): failed to open stream: No such file or directory in /home/revela25/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache.php on line 136

Warning: include(): Failed opening '/home4/aquapan/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache-base.php' for inclusion (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/revela25/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache.php on line 136

Warning: include_once(/home4/aquapan/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/ossdl-cdn.php): failed to open stream: No such file or directory in /home/revela25/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache.php on line 173

Warning: include_once(): Failed opening '/home4/aquapan/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/ossdl-cdn.php' for inclusion (include_path='.:/opt/cpanel/ea-php74/root/usr/share/pear') in /home/revela25/public_html/cjbarnaby/wp-content/plugins/wp-super-cache/wp-cache.php on line 173
Woocommerce Products Listing Page Exclude Category Filter - cjbarnaby

Woocommerce Products Listing Page Exclude Category Filter

I work in WordPress and Woocommerce often and this code was generated by asking the right questions for ChatGPT which is a very helpful AI.

It resides in your functions.php for your activated theme to apply to your product listings page in Woocommerce.

I needed something that would help me to pick out certain product items with a bit more granularity and this helped immensely.

Add it to your site at your own risk. I’m recording it here for my own use specifically however if you get use from it then that’s all good by me.

 

				
					// woocommerce exclude category filter on all products listing page

// Add filter option to exclude products from category
add_action( 'restrict_manage_posts', 'add_exclude_category_filter' );
function add_exclude_category_filter() {
    $current_cat = isset( $_GET['product_cat'] ) ? $_GET['product_cat'] : '';
    $exclude_cat = isset( $_GET['exclude_category'] ) ? $_GET['exclude_category'] : '';
    ?>
    <select name="exclude_category">
        <option value=""><?php _e( 'Exclude category', 'woocommerce' ); ?></option>
        <?php
        $categories = get_terms( 'product_cat', array(
            'hide_empty' => true,
        ) );
        foreach ( $categories as $category ) :
            $selected = $category->term_id == $exclude_cat ? 'selected="selected"' : '';
            $current = $category->term_id == $current_cat ? 'current-cat' : '';
            ?>
            <option class="<?php echo esc_attr( $current ); ?>" value="<?php echo esc_attr( $category->term_id ); ?>" <?php echo $selected; ?>>
                <?php echo esc_html( $category->name ); ?>
            </option>
        <?php endforeach; ?>
    </select>
    <?php
}

function custom_product_query( $query ) {

    // Check if we're on the products listing page
    if ( $query->is_admin && $query->query['post_type'] === 'product' && ! is_null( $query->query_vars['s'] ) ) {

        // Check if the exclude category filter is set
        if ( isset( $_GET['exclude_category'] ) && ! empty( $_GET['exclude_category'] ) ) {
            $exclude_category = absint( $_GET['exclude_category'] );
            $tax_query = array(
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id',
                    'terms'    => $exclude_category,
                    'operator' => 'NOT IN',
                ),
            );
            $query->set( 'tax_query', $tax_query );
        }
    }
}
add_action( 'pre_get_posts', 'custom_product_query' );

				
			

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment