Ottenere risultati da meta_query() tra due numeri

24 gen 2016, 15:03:32
Visualizzazioni: 17.9K
Voti: 7

Ho dei valori come:-

if ( ! empty( $_GET['filter-area'] ) ) {
    $f = $_GET['filter-area'];
    preg_match_all('!\d+!', $f, $matches);
    foreach($matches as $key) {
        $first_val = $key[0];
        $second_val = $key[1];
    }
}

Ora ho bisogno di cercare per ottenere tutti i dati tra questi due valori.

Ho provato in questo modo, ma prende solo un valore.

if ( ! empty( $_GET['filter-area'] ) ) {
    $meta[] = array(
        'key'       => REALIA_PROPERTY_PREFIX . 'attributes_area',
        'value'     => $first_val,
        'compare'   => '>=',
        'type'      => 'NUMERIC',
    );
}

Come posso cercare per due valori?

0
Tutte le risposte alla domanda 1
3
15

È possibile confrontare più valori meta con BETWEEN utilizzando un valore di tipo array:

'meta_query' => array(
    array(
        'key'     => REALIA_PROPERTY_PREFIX .'attributes_area',
        'value'   => array( $first_val, $second_val ),
        'type'    => 'numeric',
        'compare' => 'BETWEEN',
    ), // array interno.
), // array esterno.

Puoi vedere questo esempio nella Documentazione per Sviluppatori.

24 gen 2016 15:53:04
Commenti

nel caso 2,2.9 & 3,3.9 2 valori come si passano?

Ravi Patel Ravi Patel
28 feb 2018 15:05:30

Nel mio caso ho dovuto passare $first_val e $second_val in floatval() per farlo funzionare.

rAthus rAthus
13 nov 2018 18:46:53

Chiunque stia guardando questa soluzione e si chieda perché la tua meta_query non funziona, assicurati di avere array annidati (array( array( ... ))) in meta_query. Non chiedetemi quanto tempo ho impiegato a notare questa parte...

armadadrive armadadrive
22 apr 2020 23:20:47