Your question isn’t clear, but I’m making the following assumptions:
- The user is submitting
$_POST['search_option_money'];
- You want to return all
mycredit
posts where$_POST['search_option_money'];
is between theoffer_money_start
andoffer_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
.