Wordpress Orderby, Multiple acf meta fields + date doesnt work - wordpress

I'm missing something.
Trying to filter "query_two" first in DESC so items with a date are on top.
Then i want to filter "query_one" by custom meta date.
Query_two == ACF true/false field
Query_one == ACF Date field
Doesn't matter what i do it either orders by Query_two or Query_one. Not a combi. I checked if the "date fields" are empty for the first orderby... so thats not the problem.
What am i doing wrong?
function custom_query_vars( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
if ( is_post_type_archive() == 'activiteiten' ) {
$query->set( 'post_parent', 0 );
$query->set( 'posts_per_page', -1 );
$meta_query = array(
'relation' => 'OR',
'query_one' => array(
'key' => 'activi_datum',
'value' => date('Ymd'),
'type' => 'DATE',
'compare' => '>='
),
'query_two' => array(
'key' => 'activi_datumperiode',
'value' => 0,
'compare' => '='
),
);
$query->set( 'meta_query', $meta_query );
$order_by = array(
'query_two' => 'DESC',
'query_one' => 'ASC',
);
$query->set( 'orderby', $order_by );
}
}
return $query;
}

Related

Wooocommerce order "on-sale first" products inside shop and archives

i need to alter the default woocommerce filter by setting up the "on sale" products as first. The loop must contain also the not on sale, but all is ordered by on sale first.
I tried using pre_get_posts but i find the way only to filter the products (for ex display only the on sale product), not reorder it.
I solved the problem, here the solution, hope is useful for other people.
/**
* #param $q
* Reorder products on sale first on default shop filter.
*
* #return void
*/
function evolve_on_sale_reorder_query( $q ) {
$meta_query = $q->get( 'meta_query' );
if ( ! isset( $_GET['orderby'] ) ) {
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_sale_price',
'compare' => 'NOT EXISTS'
),
array(
'relation' => 'OR',
array(
'key' => '_sale_price',
'value' => '',
'compare' => "="
// 'value'=>array(''),
// 'compare' => 'IN'
),
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>=',
'type' => 'numeric',
)
),
);
$q->set( 'orderby', array( 'meta_value' => 'DESC', 'title' => 'ASC' ) );
}
$q->set( 'meta_query', $meta_query );
}
add_action( 'woocommerce_product_query', 'evolve_on_sale_reorder_query' );

Display sale items on shop page in WooCommerce

I'm trying to figure out a direct link to display all sale items in the shop. URLs can usually filter out specific attributes and queries, so I was hopeful that this would be possible. So far, no luck.
My result turns up: no products found. But there are indeed products on sale.
I've tried the following:
add_filter( 'woocommerce_product_query_meta_query', 'filter_on_sale_products', 20, 1 );
function filter_on_sale_products( $meta_query ){
if( isset($_GET['onsale']) && $_GET['onsale'] ){
$meta_query[] = array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>'
);
}
return $meta_query;
}
This should return all sale items by URL: https://www.example.com/shop/?onsale=1
Any advice would be appreciated
Your code contains some very minor errors.
You can use the woocommerce_product_query action hook instead. This should suffice:
function action_woocommerce_product_query( $q ) {
if ( is_admin() ) return;
// Isset & NOT empty
if ( isset( $_GET['onsale'] ) ) {
// Equal to 1
if ( $_GET['onsale'] == 1 ) {
// Function that returns an array containing the IDs of the products that are on sale.
$product_ids_on_sale = wc_get_product_ids_on_sale();
$q->set( 'post__in', $product_ids_on_sale );
}
}
}
add_action( 'woocommerce_product_query', 'action_woocommerce_product_query', 10, 1 );
check this link out
$args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$loop = new WP_Query( $args );

How to make WordPress/WooCommerce search function search product tags and products only?

I am having some trouble making my wordpress search query, search for WooCommerce products and tags/keywords. It would also be great to have it search categories.
Currently I've gotten it to search WooCommerce products only, but still need the tags/keywords and would also be great to get the product categories search results as well
Also the tags/keywords may very some, so it has to be dynamic, so that the customer or me don't have to update it once in a while.
// Only search woocommerce products not posts and pages
add_action( 'pre_get_posts', 'search_only_products' );
function search_only_products( $query ) {
if( ! is_admin() && is_search() && $query->is_main_query() ) {
$args = array(
);
$query->set( 'post_type', 'product', 'meta_query', array(
'relation' => 'OR',
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => 'NOT EXISTS',
),
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!=',
),
));
// $query->set($args);
}
}
// Only search woocommerce products not posts and pages
add_action( 'pre_get_posts', 'search_only_products' );
function search_only_products( $query ) {
if( ! is_admin() && is_search() && $query->is_main_query() ) {
$args = array(
);
$query->set( 'post_type', 'product', 'meta_query', array(
'relation' => 'OR',
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => 'NOT EXISTS',
),
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!=',
),
));
// $query->set($args);
}
}

Wordpress sort order by ACF text field

I have an issue that is doing my head in, I'm trying to sort my posts using pre_get_posts by and ACF field that is a text field.
Here is my code:
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
if( $query->is_main_query() && $query->is_tax('locations')) {
$query->set('meta_key', 'level');
$query->set('orderby', 'meta_value');
$query->set('order', 'DESC');
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
In the ACF config, it's set to text and the values of this text can be Bronze, Silver, Gold etc.
UPDATE
I've now changed the level field to a number and switch it round 1 = Bronze, 2 = Silver etc.
Still, I get nothing.
When I run the below nothing gets returned.
Any ideas?
I think you could be inadvertently overwriting the whole query. Try this (untested) snippet:
function my_pre_get_posts( $query ) {
if( ! is_admin() && $query->is_main_query() && $query->is_tax('locations')) {
$query->set('meta_key', 'level');
$query->set('orderby', 'meta_value');
$query->set('order', 'DESC');
}
}
add_action('pre_get_posts', 'my_pre_get_posts');
Note that $query is not returned.
Good luck!
So in the end of switched it from the functions.php file to the taxonomy template.
Here is my finished code:
<?php
if(isset($_GET['type'])) {
if($_GET['type'] == 'villa') {
$filter = array(
'key' => 'type_name',
'value' => 'Villa',
'compare' => '=',
);
} elseif ($_GET['type'] == 'apartment') {
$filter = array(
'key' => 'type_name',
'value' => 'Apartment',
'compare' => '=',
);
} elseif ($_GET['type'] == 'alls') {
$filter = array(
'relation' => 'or',
array(
'key' => 'type_name',
'value' => 'Villa',
'compare' => '=',
),
array(
'key' => 'type_name',
'value' => 'Apartment',
'compare' => '=',
)
);
}
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'properties',
'post_per_page' => 12,
'paged' => $paged,
'meta_key' => 'level',
'orderby' => 'meta_value',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'locations',
'field' => 'slug',
'terms' => $term->slug,
),
),
'meta_query' => $filter
);
// the query
$the_query = new WP_Query( $args ); ?>
Fingers crossed it helps someone else.

Filter products by date range in WooCommerce

I need to search products in WooCommerce by a date range, exactly per month. I set 2 custom fields for my products created with ACF (acf-season_from and acf-season_to).
I tried using the pre_get_posts filter but does not work properly. Here my code:
add_filter( 'pre_get_posts', function( $query ) {
if ( ! is_shop() ) { return; }
$current_m = date('m');
$month = $_GET['m'];
$year = date("Y");
if( $current_m > $month)
$year = date('Y', strtotime('+1 year'));
$query->set( 'meta_query', array(
array(
'key' => 'acf-season_from',
'value' => date( "d/m/Y", mktime( 0,0,0,date($month),date('01'),date($year))),
'compare' => '>=',
'type' => 'DATE'
),
array(
'key' => 'acf-season_to',
'value' => date( "d/m/Y", mktime(0,0,0,date($month),date("t"),date($year))),
'compare' => '<=',
'type' => 'DATE'
)
) );
return $query;} );
How can I apply the filter properly to the products loop, so only the products of selected month will be shown?
Thanks

Resources