Wordpress add attachment media alt to frontend search - wordpress

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' );

Related

Exclude products from a certain category, with Woocommerce and Elementor Anywhere

I am using Elementor Anywhere to create a specific display for my WooCommerce products.
I have a filter and for the filter works I need to use Post blocks Adv.
There I ask to display current archive.
I have the possibility to put a query filter, but I can't exclude products from a certain category.
I have this, but it doesn't work with current archives.
function filtrer_produit_sans_crea( $query_args ) {
$query_args['meta_query'] = array(
array(
'key' => '_stock_status',
'value' => 'instock',
),
);
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'coin-des-creatrices',
'operator' => 'NOT IN',
);
return $query_args;
}
add_filter( 'filtre_zero_dechet', 'filtrer_produit_sans_crea' );
Has anyone ever tried something like this?
Or do you have a clue?
OK I found a solution:
function mystore_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'coin-des-creatrices' ), // modifier la catégorie
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'mystore_pre_get_posts_query' );
But it does on the whole site.
If anyone has a clue how to do it only on a specific page...
Naively I wanted to add this:
function mystore_pre_get_posts_query( $q ) {
if ( is_page( 54 ) ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'coin-des-creatrices' ), // modifier la catégorie
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
}
add_action( 'woocommerce_product_query', 'mystore_pre_get_posts_query' );
But it's doesn't work ^_^
I found another track.
function mystore_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'coin-des-creatrices' ), // modifier la catégorie
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'wp_head', 'remove_my_action');
function remove_my_action($post) {
global $post;
$post_id = $post->ID;
if (160029 == $post_id) {
echo "PAGE OK";
add_action( 'pre_get_posts', 'mystore_pre_get_posts_query');
}else{
remove_action('pre_get_posts', 'mystore_pre_get_posts_query');
}
}
My "page ok" displays well but the function does not launch.
If it is outside this function, the line add_action('pre_get_posts', 'mystore_pre_get_posts_query');
does what I want it to do.
Is it add_action( 'wp_head', 'remove_my_action'); that is not the right one?

How Can I Order The Results Of Meta Query? - woocommerce_product_query_meta_query

I have the following code:
function custom_meta_query( $meta_query ){
$rem_width = get_query_var('rem_width');
$rem_length = get_query_var('rem_length');
$meta_query[] = array( 'key'=>'remnant_width', 'value' => $rem_width, 'compare'=>'>=');
$meta_query[] = array( 'key'=>'remnant_length', 'value' => $rem_length, 'compare'=>'>=');
$meta_query['relation'] = 'AND';
return $meta_query;
}
// The main shop and archives meta query
add_filter( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $meta_query, $query ) {
if( (! is_admin() ) && ((isset($_GET['rem_width'])) || (isset($_GET['rem_length']) ) ))
return custom_meta_query( $meta_query );
}
I want to order the products returned by width and currently it is showing alphabetical order.
'orderby' => 'meta_value_num',
'meta_key' => 'remnant_width',
'order' => 'ASC'
But I don't know how to incorporate it into what has gone before.
Thanks

Exclude category from search results [duplicate]

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' );

Pagenavi by multiple field search wordpress

I am using the pagenavi plugin and mutiple search by taxonomy and acf custom field. However pagenavi is not working on the query search.
add_filter( 'pre_get_posts','nt_custom_search_filter');
function nt_custom_search_filter( $query ) {
if( $query->is_search && !is_admin() ) {
if( isset($_GET['s'] ) ) {
$query->set( 'post_type', array( 'product' ) );// box text search
}
if( $query->is_main_query() ) {
$args[] = array( 'relation' => 'AND' );
if( isset( $_GET[ 'product_cat' ] ) && $_GET[ 'product_cat' ] != 'all' ) {
$args[] =
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $_GET['product_cat']
);
}$query->set( 'tax_query', $args );
$meta_query = array( 'relation' => 'AND' );
if( isset( $_GET[ 'status' ] ) && $_GET[ 'status' ] != 'all' ) {
$meta_query[] = array(
'key' => 'status',
'value' => $_GET['status'],
'compare' => '='
);
}
$query->set( 'meta_query', $meta_query );
}
}
return $query;}
To solve possible problems with an incorrect calculation of the pagination in WP_Query, you need three additional arguments: posts_per_page, paged and offset
In $count we save the number of posts per page, or just use the default which you have set in your wordpress backend under the settings.
$paged tells us which page the user is currently on.
With $offset you calculate how many contributions the loop should be moved.
So you can adjust the $args[] in your $query. Code can look like:
$count = get_option('posts_per_page', 25); // get 25 posts
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$offset = ($paged - 1) * $count;
$args = array (
'posts_per_page' => $count,
'paged' => $paged,
'offset' => $offset,
'relation' => 'AND'
.
. /* other arguments */
:
);
Edit:
so your code can look like the following. Please notice, that the code is not tested.
add_filter( 'pre_get_posts','nt_custom_search_filter');
function nt_custom_search_filter( $query ) {
if( $query->is_search && !is_admin() ) {
if( isset($_GET['s'] ) ) {
$query->set( 'post_type', array( 'product' ) );// box text search
}
if( $query->is_main_query() ) {
$count = get_option('posts_per_page', 25); // get 25 posts
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$offset = ($paged - 1) * $count;
$args[] = array( 'posts_per_page' => $count, 'paged' => $paged, 'offset' => $offset, 'relation' => 'AND' );
if( isset( $_GET[ 'product_cat' ] ) && $_GET[ 'product_cat' ] != 'all' ) {
$args[] =
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $_GET['product_cat']
);
}$query->set( 'tax_query', $args );
$meta_query = array( 'relation' => 'AND' );
if( isset( $_GET[ 'status' ] ) && $_GET[ 'status' ] != 'all' ) {
$meta_query[] = array(
'key' => 'status',
'value' => $_GET['status'],
'compare' => '='
);
}
$query->set( 'meta_query', $meta_query );
}
}
return $query;}

Woocommerce : only show products between start and end dates

I'm trying to display only products between two dates. When I only put 1 array and one date it works, but when I put other arrays and my second date it doesn't work anymore, it shows all the products. Any idea ?
function custom_meta_query( $meta_query ){
$today = current_time('Ymd');
$args = array (
'meta_query' => array(
'relation' => 'AND',
array(
'key'=>'flash_sale_start',
'value' => $today,
'compare'=>'<=',
'type' => 'DATE'
),
array(
'key'=>'flash_sale_end',
'value' => $today,
'compare'=>'>=',
'type' => 'DATE'
)),);
$date_query = new WP_Query( $args );
//return $meta_query;
}
// The main shop and archives meta query
add_filter( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $meta_query, $query ) {
if( ! is_admin() )
return custom_meta_query( $meta_query );
}
// The shortcode products query
add_filter( 'woocommerce_shortcode_products_query', 'custom__shortcode_products_query', 10, 3 );
function custom__shortcode_products_query( $query_args, $atts, $loop_name ) {
if( ! is_admin() )
$query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
return $query_args;
}
// The widget products query
add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
if( ! is_admin() )
$query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
return $query_args;
}
Thank you
I've found the solution, it was an array() problem
function custom_meta_query( $meta_query ){
$today = current_time('Ymd');
$args = array (
'numberposts' => -1,
'meta_query' => array(
'relation' => 'AND',
'start_clause' => array(
'key'=>'flash_sale_start',
'value' => $today,
'compare'=> '<=',
'type' => 'DATE'
),
'end_clause' => array(
'key' => 'flash_sale_end',
'value' => $today,
'compare' => '>=',
'type' => 'DATE'
),
));
return $args;
}
// The main shop and archives meta query
add_filter( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $meta_query, $query ) {
if( ! is_admin() )
return custom_meta_query( $meta_query );
}
// The shortcode products query
add_filter( 'woocommerce_shortcode_products_query', 'custom__shortcode_products_query', 10, 3 );
function custom__shortcode_products_query( $query_args, $atts, $loop_name ) {
if( ! is_admin() )
$query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
return $query_args;
}
// The widget products query
add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
if( ! is_admin() )
$query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
return $query_args;
}

Resources