I have this situation. i'm building a WordPress site to sell some products. The thing is that the price of the products is stored as meta value, but the product price can be in difference currency. Now the problem is that I have to make a price filter for all product no matter in what currency is the price, the filter currency is configured in the background, and the the product that didn't have it's price in that currency have to be filter by the currency exchange tax, but i really don´t know how to build a meta_query were i can filter by currency equivalent price.
For example, I have this 2 products:
Product 1
-price:10
-currency: usd
Product 2
- price: 1000
- currency: xxx
the filter is set to use usd and find price between 0 and 10. So for this time the search will return product 1, but the currency conversion from xxx to usd is 500 to 1, the product 2 equivalent price is 2 usd the it have to be included on the search result.
until now this is the part of my code to handle the price filter:
if (
isset($_REQUEST['max_price']) && $_REQUEST['max_price'] &&
isset($_REQUEST['min_price']) && ($_REQUEST['min_price'] || $_REQUEST['min_price'] == 0)
) {
$main_currency = cs_get_option('main_currency');
$ex_currency = array_diff(['xxx', 'usd'], [$main_currency])[1];
$args['meta_query'] [] =
array(
'relation' => 'AND',
array(
'key' => 'price_meta_key',
'value' => array($_REQUEST['min_price'], $_REQUEST['max_price']),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
array(
'key' => 'product_currency',
'value' => $main_currency,
'compare' => '='
)
);
}
but this only filter the product with the $main_currency currency.
You can build up query that matches multiple currencies:
// Define currencies and exchange rates
$currencies = array(
'usd' => 1,
'eur' => 0.86
);
// Generate a term for each currency
$terms = array();
foreach ($currencies as $currency => $rate) {
// Match the currency code and the converted price range
$terms[] = array(
'relation' => 'AND',
array(
'key' => 'product_currency',
'value' => $currency
'compare' => '='
),
array(
'key' => 'price_meta_key',
'value' => array(
$_REQUEST['min_price'] * $rate,
$_REQUEST['max_price'] * $rate
),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
);
}
// Finally OR the terms together
$args['meta_query'][] = array(
'relation' => 'OR',
$terms
);
Related
I have a meta_key called delivered_date, in date format, example 2021-05-13 16:01:26.
If I MUST use wc_get_orders( $args ), how can I get all orders within a date range?
Tried using the meta_compare argument but it returns nothing:
// The 2 variables below will come from a datepicker widget
$start_date = "2021-05-10 0:00:00";
$end_date = "2021-05-30 11:59:59";
$args = array(
'type' => 'shop_order',
'limit' => 100,
'paginate' => true,
'page' => 1,
'orderby' => 'id',
'order' => 'DESC',
'meta_key' => 'delivered_date',
'meta_value' => array($start_date, $end_date),
'meta_compare' => 'BETWEEN',
);
$result = wc_get_orders( $args );
You are close, use date — Format a local time/date
So you get:
// The 2 variables below will come from a datepicker widget
$start_date = date( '2021-05-10 00:00:00' );
$end_date = date( '2021-05-30 11:59:59' );
$args = array(
'orderby' => 'id',
'order' => 'DESC',
'meta_key' => 'delivered_date',
'meta_value' => array( $start_date, $end_date ),
'meta_compare' => 'BETWEEN',
);
$orders = wc_get_orders( $args );
// NOT empty
if ( ! empty ( $orders ) ) {
foreach ( $orders as $order ) {
echo '<p>ID = ' . $order->get_id() . '</p>';
}
}
Result: ID = 2525, ID = 2524, ID = 2523
I would like to retrieve posts based on the current month.
My customs post type has a start date and an end date.
If the current month is between this period, then retrieve the items.
Someone can help me ?
$date_month = (new DateTime)->format('m');
$start = get_field('start_date');
$end = get_field('start_end');
// parameters of $upcoming
$args_upcoming_period = array(
'posts_per_page' => 10,
'post_type' => array('sports', 'culture'),
'meta_query' => array(
array(
'key' => $date_month,
'compare' => 'BETWEEN',
'value' => array($start, $end),
)
),
);
$upcoming_period = new WP_Query($args_upcoming_period);
you can use this code:
$start_date = "here_value"; //e.g. 2019-01-01
$end_date = "here_value"; //e.g. 2019-02-01
$metaKey = "here_value"; //e.g. date
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'meta_query' => array(
array(
'key' => $metaKey,
'value' => array($start_date, $end_date),
'compare' => 'BETWEEN',
'type' => 'DATE'
),
)
);
$result = new WP_Query($args);
Please note, var start_date & end_date they must be in the sql format (e.g. YYYY-MM-DD).
And meta_key compile with field you want filter. (e.g. date)
I'm trying to implement a filter on wp_query to show annual archives by School Year (sept - may) rather than jan-dec. When I view a yearly archive page, such as sitename.com/2017/, I get a white screen of death, and no PHP errors.
My code in functions.php looks like the following:
add_action( 'pre_get_posts', 'school_year_filter' );
function school_year_filter($query) {
if (! is_admin() && $query->is_main_query() && $query->is_year() && $query->is_archive()) {
$year = get_query_var('year');
$year2 = $year + 1;
$query->set(
'date_query' => array(
array(
'after' => array(
'year' => "$year",
'month' => '9',
'day' => '1',
),
'before' => array(
'year' => "$year2",
'month' => '5',
'day' => '31',
),
'inclusive' => true,
),
),
);
}
}
Try this code.
I changed this line 'date_query'=>array( to 'date_query', array(.
add_action( 'pre_get_posts', 'school_year_filter' );
function school_year_filter($query) {
if (! is_admin() && $query->is_main_query() && $query->is_year() && $query->is_archive()) {
$year = get_query_var('year');
$year2 = $year + 1;
$query->set(
'date_query', array(
array(
'after' => array(
'year' => $year,
'month' => '9',
'day' => '1',
),
'before' => array(
'year' => $year2,
'month' => '5',
'day' => '31',
),
'inclusive' => true,
),
)
);
}
}
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Only_Display_Search_Results_After_Specific_Date
I have 100 posts all have a meta_key named "isactive" whose value will either be "1" or "0"
I need to calculate the percentage of number of active posts(from 'isactive' metakey value) between any 2 dates
$args0 = array(
'date_query' => array(
array(
'after' => 'January 1st, 2013',
'before' => array(
'year' => 2013,
'month' => 2,
'day' => 28,
),
'inclusive' => true,
),
'meta_key' => 'isactive',
'meta_value' => 0
)
);
$count0 = new WP_Query( $args0 );
$args1 = array(
'date_query' => array(
array(
'after' => 'January 1st, 2013',
'before' => array(
'year' => 2013,
'month' => 2,
'day' => 28,
),
'inclusive' => true,
),
'meta_key' => 'isactive',
'meta_value' => 1
)
);
$count1 = new WP_Query( $args1 );
echo $percent = $count0/$count1;
Whether this type of query works ?
Else is there any better way to calculate percentage based on the meta key values between 2 dates?
I have "product" custom post type. It have 2 custom fields: price1, price2 (price of product)
I want to get all posts have price1 < price2 (compare 2 values)
How can i do that?
Here's a nice sample that should help you out:
$args = array(
'tax_query' => array(
'taxonomy' => 'custom_taxonomy_name',
array(
'key' => 'price',
'value' => array( 100, 200 ),
'compare' => 'BETWEEN',
'type' => 'numeric',
),
array(
'key' => 'description',
'value' => 'round',
'compare' => 'NOT LIKE'
)
)
);
$query = new WP_Query($args);