wp query - Get posts having meta value between two numbers

admin2025-06-02  1

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 :)

Share Improve this question edited Mar 15, 2019 at 11:44 Tomasz Walas asked Mar 8, 2019 at 10:33 Tomasz WalasTomasz Walas 34 bronze badges 4
  • 1 Maybe I don't understand, but this seems backwards. You're asking the database to run a BETWEEN search with two strings. Are offer_money_start and end supposed to be ACF fields for this post? Please explain in more depth what you are trying to do. – tmdesigned Commented Mar 8, 2019 at 10:40
  • offer_money_start and end is number – Tomasz Walas Commented Mar 8, 2019 at 11:25
  • change decimal to numeric – Gufran Hasan Commented Mar 8, 2019 at 11:33
  • Now i have all records and it's not working – Tomasz Walas Commented Mar 8, 2019 at 11:44
Add a comment  | 

1 Answer 1

Reset to default 0

Your question isn't clear, but I'm making the following assumptions:

  1. The user is submitting $_POST['search_option_money'];
  2. You want to return all 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748803511a313851.html

最新回复(0)