have a problem with meta_query_args
.
<?php
$money_form = 0;
$money_form = $_POST['search_option_money'];
if( $money_form != 0 ){
$meta_query_args = array(
array(
'key' => $money_form,
'value' => array(
'offer_money_start',
'offer_money_end'
),
'type' => 'decimal',
'compare' => 'BETWEEN' ///BETWEEN
),
);
} else {
//
}
$myquery = new WP_Query( array(
'post_type' => 'mycredit',
'posts_per_page' => -1,
'orderby' => 'rand',
'meta_query' => $meta_query_args
) );
I need all the offer which is between start and end, but I have no results. I don't know why?
All its ok thanks :)
have a problem with meta_query_args
.
<?php
$money_form = 0;
$money_form = $_POST['search_option_money'];
if( $money_form != 0 ){
$meta_query_args = array(
array(
'key' => $money_form,
'value' => array(
'offer_money_start',
'offer_money_end'
),
'type' => 'decimal',
'compare' => 'BETWEEN' ///BETWEEN
),
);
} else {
//
}
$myquery = new WP_Query( array(
'post_type' => 'mycredit',
'posts_per_page' => -1,
'orderby' => 'rand',
'meta_query' => $meta_query_args
) );
I need all the offer which is between start and end, but I have no results. I don't know why?
All its ok thanks :)
Your question isn't clear, but I'm making the following assumptions:
$_POST['search_option_money'];
mycredit
posts where $_POST['search_option_money'];
is between the offer_money_start
and offer_money_end
custom fields.The problem is that you're misusing the key
and value
arguments of the meta query. The key
is the name of the custom field, and value
is the value you're searching for.
Also, the BETWEEN
meta query is when you want to find posts based on a meta key's value being between 2 specified values, not for finding posts based on a value between 2 separate meta keys.
If you want to find posts where a given value is between the value of 2 separate meta keys, you need 2 separate meta queries:
$query_args = [
'post_type' => 'mycredit',
'posts_per_page' => -1
'orderby' => 'rand',
];
if ( isset( $_POST['search_option_money'] ) ) {
$money_form = $_POST['search_option_money'];
$query_args['meta_query'] = [
'relation' => 'AND',
[
'key' => 'offer_money_start',
'value' => $money_form,
'compare' => '>=',
'type' => 'DECIMAL',
],
[
'key' => 'offer_money_end',
'value' => $money_form,
'compare' => '<=',
'type' => 'DECIMAL',
],
]
}
$my_query = new WP_Query( $query_args );
This will query any posts where the submitted value from the form is greater than the offer_money_start
custom field, but less than the offer_money_end
custom field.
I've also formatted the code so that the meta query is properly set only when $_POST['search_option_money']
exists. Your current code will assign meta_query
to an undefined variable if it doesn't.
Lastly, POST requests are inappropriate for getting data, which is what searching or filtering is doing. Your form should be use the GET
method, and $_POST
should be $_GET
.
decimal
tonumeric
– Gufran Hasan Commented Mar 8, 2019 at 11:33