I am trying to insert a WooCommerce element to display "products" (courses) from a specific category into a page.
I had also to hide these products for this specific category and it worked as expected. I just added a a filter inside functions.php and was it:
/*
* Exclude "packages" category from "Archive Products" on page 811
* Ref. URL: https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/
*/
function exclude_category_archive_products( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'packages' ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
if( $current_post_id != "811" ) {
add_filter( 'woocommerce_product_query', 'exclude_category_archive_products' );
}
/* END Exclude "packages" category from "Archive Products" on page 811 */
I have searched for ways to to achieve the opposite and I did not find anything "from this year or close". I've tried to use the "IN" or "=" operator but it didn't work (it displays everything):
/*
* Display "packages" category only
*/
function show_only_category_in_page( $q ) {
var_dump("It reaches the function");
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'packages' ),
'operator' => '='
);
$q->set( 'tax_query', $tax_query );
}
if( $current_post_id == "811" ) {
var_dump("It reaches the page");
add_filter( 'woocommerce_product_query', 'show_only_category_in_page' );
}
/* END Display "packages" category only */
The previous code writes the string(23) "It gets reachs the page"
only. What am I doing wrong?