Exclude category from search results [duplicate] - wordpress

I'm using this code to exclude some post category from the wordpress search results :
function SearchFilter($query)
{
if ($query->is_search)
{
$query->set('cat', '-709,-710,-614');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
My problem is that it doesn't work for woocommerce categories and products are not filtered.
How can I filter some woocommerce categories too ?

Can you replace your category. hope this help you.
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' => 'category-2',
'operator' => 'NOT IN',
),
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'wpse188669_pre_get_posts' );

Related

Exclude posts that belong to a specific custom taxonomy term even if it belong to another taxonomy term

I would like to exclude posts that belong to a specific custom taxonomy term "for example id=37" (and childs) even if it belong to another(s) taxonomy term.
Here is my function :
add_action( 'pre_get_posts', 'films_query' );
function films_query( $query ) {
if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'films' ) ) {
$taxquery = array(
array(
'taxonomy' => 'categorie_films',
'field' => 'term_id',
'terms' => 37,
'operator' => 'NOT IN',
'include_children' => true,
)
);
$query->set( 'posts_per_page', '-1' );
$query->set( 'post_status', 'publish' );
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
Any help appreciated.
Best.
Pierre

Woocommerce > Hide products that are out of stock BUT skip products with certain tag

I would like to hide products that are out of stock on archive pages.
But products containing tag "backorder" should be skipped.
I tried many options but the problem is that I can't combine meta_query and tax_query to do this.
Does anyone have suggestion on how to do this?
This is my current status:
add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
function shop_only_instock_products( $meta_query, $query ) {
if( is_admin() || is_search() || ! is_shop()) return $meta_query;
$meta_query[] = array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '!='
);
print_r($meta_query);
}
add_filter( 'woocommerce_product_query_tax_query', 'filter_products_with_specific_product_tags', 9, 2 );
function filter_products_with_specific_product_tags( $tax_query, $query ) {
if( is_admin() || is_search() || ! is_shop()) return $tax_query;
$tax_query[] = array(
'taxonomy' => 'product_tag',
'field' => 'name',
'terms' => array('backorder'),
);
return $tax_query;
};
You can try woocommerce_product_query hook. I've not tested this code yet.
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
// Ignore this condition and test the behind codes firstly
// if( is_admin() || is_search() || ! is_shop()) return;
// Get any existing Tax query
$tax_query = $q->get( 'tax_query');
// Get any existing meta query
$meta_query = $q->get( 'meta_query');
// Define an additional tax query
$tax_query = array(
'taxonomy' => 'product_tag',
'field' => 'name', // backorder looks like slug
'terms' => array('backorder'),
'compare' => 'IN',
);
// Define an additional meta query
$meta_query = array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '!=',
);
// Set the new merged tax query
$q->set( 'tax_query', $tax_query );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
You should use on backorder setting in your product inverntory. Set Out of stock visibility to true in your Woocommerce > Settings > Product > Inventory . You dont need any additional code for this.

Hide completely from anywhere on my site any product that has no category. : wooCommerce

In my wooCommerce platform, I do not want to show products that do not have a category selected. I mean which products categories is empty that's products are not display in my site.
Is it any way to do that?
There are different ways to retrieve products, and if you want to exclude products which have no category assigned to from everywhere in your site, then you will need to address all of them.
WP_Query
You can hook into the pre_get_posts action and modify the tax_query arg in order to exclude products in the uncategorized product category (only when the query post type is product). I'm in fact assuming uncategorized being the slug of the default product category (you will need to amend it to your specific configuration). E.g.:
function remove_uncategorized_products( $query ) {
if ( is_admin() ) {
return;
}
if ( 'product' !== $query->get( 'post_type' ) ) {
return;
}
$tax_query = (array) $query->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'uncategorized' ),
'operator' => 'NOT IN',
);
$query->set( 'tax_query', $tax_query );
}
add_action( 'pre_get_posts', 'remove_uncategorized_products' );
WC_Query
Similar to WP_Query, you can hook into the woocommerce_product_query action to modify the query. E.g.:
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'uncategorized' ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
WC_Product_Query (used by wc_get_products)
In this case, we can't change the query args to exclude certain product categories. We can instead loop through each product returned by the query and check its category. E.g.:
function filter_uncategorized_products_out( $results, $args ) {
$products = array();
foreach ( $results as $p ) {
$is_uncategorized = false;
$terms = get_the_terms( $p->get_id(), 'product_cat' );
foreach ( $terms as $term ) {
if ( 'uncategorized' === $term->slug ) {
$is_uncategorized = true;
}
}
if ( ! $is_uncategorized ) {
$products[] = $p;
}
}
return $products;
}
add_filter( 'woocommerce_product_object_query', 'filter_uncategorized_products_out', 10, 2 );
Please note that there are other ways to retrieve products (for example using $wpdb directly). You might need to check all the pages of your site to see if you have covered them all.

Wordpress add attachment media alt to frontend search

I'm trying to figure out a way to add the media alt tags to my frontend search. So far I've found the following, but I haven't been able to figure out how to include the alt tags to the query_set:
function attachment_search( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post', 'attachment' ) );
$query->set( 'post_status', array( 'publish', 'inherit' ) );
}
return $query;
}
add_filter( 'pre_get_posts', 'attachment_search' );
Try this function instead.
function attachment_search( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post', 'attachment' ) );
$query->set( 'post_status', array( 'publish', 'inherit' ) );
$meta_query_args = array(
array(
'key' => '_wp_attachment_image_alt',
'value' => $query->query_vars['s'] = '',
'compare' => 'LIKE'
)
);
$query->set('meta_query', $meta_query_args);
}
return $query;
}
add_filter( 'pre_get_posts', 'attachment_search' );

Woocommerce product search in admin is not working

I have a list of products in woocommerce
when I search for a product (in admin), it always says "No Products found":
This is the URL /wp-admin/edit.php?s=Deluxe&post_status=all&post_type=product&action=-1&m=0&product_cat&product_type&paged=1&action2=-1
and when I remove product_cat from the URL the product comes up
Add in the following snippet to function.php:
add_action( 'pre_get_posts', 'products_pre_get_posts' );
function products_pre_get_posts( $query ) {
if(is_admin()){
$query->set( 'tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => get_terms( array( 'taxonomy' => 'product_cat', 'fields' => 'ids' ) )
)
));
}
}
I had the same problem. After disable the YoastSeo plugin, the search works properly again.
The Answer from Chandrakant Devani helped, but broke other searches in the admin. Adding an if seems to avoid breakages
if ( is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'product')
full code:
add_action( 'pre_get_posts', 'products_pre_get_posts' );
function products_pre_get_posts( WP_Query $query ) {
if ( is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'product' ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => get_terms( array( 'taxonomy' => 'product_cat', 'fields' => 'ids' ) )
)
) );
}
}

Resources