Interrogare più valori chiave meta?


22

Come eseguire una query per valori di meta-chiave multipli con la stessa chiave

$querystr = "  
            SELECT $wpdb->posts.* 
            FROM $wpdb->posts, $wpdb->postmeta
            WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 

            AND $wpdb->postmeta.meta_key = 'key1'   
            AND $wpdb->postmeta.meta_value = 'value1'
            // why doesn't this work?
            AND $wpdb->postmeta.meta_value = 'value2'

            AND $wpdb->posts.post_status = 'publish' 
            AND $wpdb->posts.post_type = 'post'
            ORDER BY $wpdb->posts.post_date DESC
                ";

codice successivo

<?php 
$args = array(
    'meta_query' => array(
        array(
            'key' => 'key1',
            'value' => 'value1',
            'compare' => '='
        ),
// this array results in no return for both arrays
        array(
            'key' => 'key1',
            'value' => 'value2',
            'compare' => '='
        )
    )
);
$the_query  = new WP_Query( $args );
 ?>

                <?php /* Start the Loop */ ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                    <?php get_template_part( 'content', get_post_format() ); ?>

                <?php endwhile; ?>

Risposte:


31

Sento che c'è una confusione AND / OR in corso qui.

Le query nel PO restituiranno solo i post che hanno sia key1 = 'value1' AND key2 = 'value2'. La maggior parte dei plugin WP (di cui sono a conoscenza, comunque) non memorizza più valori in postmeta, per lo stesso post, usando la stessa chiave.

Se quello che vuoi è davvero un OR (vuoi ottenere i post dove key1 = 'value1', così come i post in cui key1 = 'value2'), allora vedi la risposta di @ WhiskerSandwich, usando 'IN' e una matrice di valori per il parametro valore.

In alternativa, puoi fornire un relationparametro a `meta_query ':

$args = array(
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'key1',
            'value' => 'value1',
            'compare' => '='
        ),

        array(
            'key' => 'key1',
            'value' => 'value2',
            'compare' => '='
        )
    )
);

Si noti che l'utilizzo di OR come relazione per più meta query utilizzando la stessa chiave è l'equivalente funzionale dell'utilizzo INe una matrice di valori per una singola.


Grazie per questo, Boone. Non sapevo che esistesse il parametro "relazione". Mi ha aiutato
— MathSmath,

Funziona se hai solo una chiave su cui cercare. Se ne hai due o più, potresti dover usare 'AND' per combinarli nel parametro di relazione, nel qual caso la risposta di @ WhiskerSandwich sotto è la soluzione migliore.
— SinisterBeard,

14

Ho avuto lo stesso problema in cui il passaggio di più array per la stessa chiave non funzionava. Invece, basta usare un array, impostare 'value' su un array di valori e impostare 'compare' su IN:

<?php

$args = array(
    'meta_query' => array(
        array(
            'key' => 'key1',
            'value' => array('value1', 'value2'),
            'compare' => 'IN'
        ),
    )
);
$query = new WP_Query( $args );

?>

1

Devi alias la tabella postmeta per il secondo valore:

$querystr = "  
        SELECT $wpdb->posts.* 
        FROM $wpdb->posts, $wpdb->postmeta, $wpdb->postmeta AS mt1
        WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 

        AND $wpdb->postmeta.meta_key = 'key1'   
        AND $wpdb->postmeta.meta_value = 'value1'
        AND mt1.meta_key = 'key1'
        AND mt1.meta_value = 'value2'

        AND $wpdb->posts.post_status = 'publish' 
        AND $wpdb->posts.post_type = 'post'
        ORDER BY $wpdb->posts.post_date DESC
            ";

Puoi anche farlo ora dal 3.1 con un meta_query:

$args = array(
    'meta_query' => array(
        array(
            'key' => 'key1',
            'value' => 'value1',
            'compare' => '='
        ),
        array(
            'key' => 'key1',
            'value' => 'value2',
            'compare' => '='
        )
    )
);
$query = new WP_Query( $args );

Ciao Milo grazie per aver risposto. L'SQL non restituisce alcun valore. E l'array non restituisce alcun valore, a meno che non rimuova la seconda chiave e il valore dall'array. Quindi questo è infastidito?
— rimasto il

@steen - Non sono sicuro di quale sia il tuo problema, ho testato entrambi i metodi e funzionano nella mia installazione di 3.3.1. La tua chiave è letteralmente 'chiave1' e i valori 'valore1' e 'valore2'? Non vedi nulla se print_r( $the_query );immediatamente dopo la query?
— Milo,

0

Key è key1 e i valori 'value1' e 'value2' lo hanno provato sia in formato testo che numerico in una nuova installazione con ventiundici. print_r ($ the_query); funziona l'uscita sembra normale. Anche provato key1 e key2 non funziona. Funziona non appena lo limito a un array. Controllato con diversi browser.

Questo tuttavia funziona.

    <?php 
$args = array(
    'meta_query' => array(
        array(
            'key' => 'wtf',
            'value' => '1',
            'compare' => '>='
        ),
// this array results in no return for both arrays
        array(
            'key' => 'wtf',
            'value' => '2',
            'compare' => '<='
        )
    )
);
$the_query  = new WP_Query( $args );

 ?>
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.