0

I have a dynamic form that brings the data from the URL and searches the DB with a wp_Query. It works great if I insert a dingle value manually. but if I pass the data from the URL I receive all the posts. even if I manually insert an array with multiple values it returns all posts.

here is the query: the first part works great. the second one cost_per_month_ipcenter_filter_new is the one with the problem.

    $args = array(
    'posts_per_page' => -1,
    'post_status' => 'publish',
    's' => $_GET['s'],
    'post_type' => 'ipcenters',
    'meta_query'     =>  array(
         'relation '  => 'AND',
        array(
            'meta_key'      => 'setupfee_ipcenter',
            'meta_value'        => $setupfee_ipcenter_val,
            'meta_compare'   => $setupfee_ipcenter_comp,
        ),
        array(
            'key'       => 'cost_per_month_ipcenter_filter_new',
            'value'     =>  array('101-150', '0-50'),
            'compare'   => '=',

        )
    ),
);


$the_query = new WP_Query( $args );

if I write

'value' => '101-150',

it works. I tried 'value' => ['101-150', '0-50'], This is the same as the written above but it still doesn't work.

this is how it appears in the url cost_per_month_ipcenter_filter_new%5B%5D=0-50&cost_per_month_ipcenter_filter_new%5B%5D=101-150

the final result it to place the $_GET['cost_per_month_ipcenter_filter_new'] as the value array.

What am I doing wrong?

2
  • 1
    "the first part works great" - are you sure? Because meta query clauses (arrays in the meta_query arg) use key, value and compare. I.e. Without the meta_ part. And 'value' => '101-150' works because the value is a string. So when it's an array of values, compare should be IN, i.e. 'compare' => 'IN'.
    – Sally CJ
    Commented Aug 21, 2021 at 13:05
  • 1
    Thanks! it really was the compare value, the IN really fixed it
    – DavSev
    Commented Aug 22, 2021 at 6:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.