$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Get posts having meta value between two numbers|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Get posts having meta value between two numbers

matteradmin9PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far