2013-07-19 10 views
14

Sto utilizzando un ciclo personalizzato per visualizzare i miei eventi in una pagina, ho capito bene ordinazione dall'evento data di iniziare ad utilizzare il sottostante:Come ordinare per più meta chiavi?

$args = array( 
    'post_type' => 'event', 
    'order' => 'ASC', 
    'orderby' => 'meta_value', 
    'meta_key' => '_event_start_date'); 
$loop = new WP_Query($args); 

Ma l'opzione meta_key consente un solo valore. Come utilizzare due valori (_event_start_date e _event_start_time)?

+0

Anche io lo sto cercando, e temo che solo il WP4.0 lo consenta. Ho provato tutte le possibili configurazioni dei parametri di Query senza successo su 3.9 ... – Ninj

risposta

-1

Da WP_Query documentation, utilizzare meta_query e riempire con una serie di chiavi/valori meta:

$args = array(
    'post_type' => 'product', 
    'meta_query' => array(
     array(
      'key' => 'color', 
      'value' => 'blue', 
      'compare' => 'NOT LIKE' 
     ), 
     array(
      'key' => 'price', 
      'value' => array(20, 100), 
      'type' => 'numeric', 
      'compare' => 'BETWEEN' 
     ) 
    ) 
); 
$query = new WP_Query($args); 
+3

Ma questo non aiuta nell'ordinare, vero? – user1280853

+0

Non proprio sicuro, controlla [questa query di ricerca] (http://wordpress.stackexchange.com/search?q=meta_query+is%3Aanswer). – brasofilo

+0

La mia risposta qui sotto chiarisce questo, per quanto riguarda l'ordine con più query meta chiave. –

-1

È possibile ordinare su una chiave, di più. In questo modo:

 $events = get_posts(array(
      'post_type' => 'my_event', 
      'post_status' => 'publish', 
      'meta_query' => array(
       array(
        'key' => 'my_event_type', 
        'value' => 'aGoodOne' 
       ), 

       array(
        'key' => 'my_event_start', 
        'value' => time(), // in this case, only grabbing upcoming events 
        'compare'=> '>', 
        'type' => 'NUMERIC' // assuming value is unix timestamp 
       ) 
      ), 
      'meta_key' => 'my_event_start', // we're sorting on the start time key 
      'orderby' => 'meta_value_num', // it's a numeric value 
      'order'  => 'ASC' // in chrono order 
     )); 

La chiave qui è che meta_key specifica quale chiave che ordinano da, di quelli che usiamo per afferrare i nostri dischi con meta_query.

+8

Utile come questo, non risponde alla domanda originale. Questo risponde "Se interrogo da più meta chiavi, come posso ordinare da una singola meta chiave?" quello che è stato chiesto è stato "Sto inserendo messaggi e ho bisogno di ordinarli usando più meta chiavi". –

-1

add_action('pre_get_posts', 'pre_get_posts_programpunkter'); 
 

 
function pre_get_posts_programpunkter($query) { 
 
\t 
 
\t if(!is_admin() && is_post_type_archive('programpunkt') && $query->is_main_query()) { 
 
\t \t 
 
\t \t $query->set('orderby', 'meta_value'); 
 
\t \t $query->set('meta_key', 'event-date'); 
 
\t \t \t 
 
\t \t $query->set('meta_query', array(
 
\t \t \t 'relation' => 'AND', 
 
\t \t \t \t array('key' => 'event-date', 'compare' => 'EXISTS'), 
 
\t \t \t \t array('key' => 'event-start', 'compare' => 'EXISTS') 
 
\t \t \t) 
 
\t \t); 
 
\t \t \t 
 
\t \t $query->set('order', 'ASC'); 
 
\t \t \t \t 
 
\t } 
 
} 
 

 
add_filter('posts_orderby', 'programpunkter_orderby'); 
 

 
function programpunkter_orderby($orderby) { 
 
\t 
 
\t if(get_queried_object()->query_var === 'programpunkt') { 
 
\t \t 
 
\t \t global $wpdb; \t \t 
 
\t \t $orderby = str_replace($wpdb->prefix.'postmeta.meta_value', 'mt1.meta_value, mt2.meta_value', $orderby); \t 
 
\t \t 
 
\t } \t 
 
\t return $orderby; 
 
}

+0

Sebbene questi collegamenti possano rispondere alla domanda, è meglio includere qui le parti essenziali della risposta e fornire i collegamenti per riferimento. Le risposte di solo collegamento possono diventare non valide se le pagine collegate cambiano. – matsjoyce

0

devi usare meta_query a aggiungere le chiavi del meta come detto tutti, ma anche si deve personalizzare ordine in base alla funzione.

$args = array(
    'post_type' => 'event', 
    'meta_key' => '_event_start_date', 
    'meta_query' => array(
     array('key' => '_event_start_date'), 
     array('key' => '_event_start_time') 
    ), 
); 

add_filter('posts_orderby','customorderby'); 
$loop = new WP_Query($args); 
remove_filter('posts_orderby','customorderby'); 

e mettere questo sul tuo functions.php

function customorderby($orderby) { 
    return 'mt1.meta_value ASC, mt2.meta_value ASC'; 
} 

Si può leggere di più a http://dotnordic.se/sorting-wordpress-posts-by-several-numeric-custom-fields/

7

Questo è qualcosa che WordPress aggiunto il supporto per 4.2: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

Nella tua caso si vorrà probabilmente fare qualcosa di simile:

$args = array(
    'post_type' => 'event', 
    'meta_query' => array(
     'relation' => 'AND', 
     'event_start_date_clause' => array(
      'key'  => '_event_start_date', 
      'compare' => 'EXISTS', 
     ), 
     'event_start_time_clause' => array(
      'key'  => '_event_start_time', 
      'compare' => 'EXISTS', 
     ), 
    ), 
    'orderby' => array(
     'event_start_date_clause' => 'ASC', 
     'event_start_time_clause' => 'ASC', 
    ), 
); 

$loop = new WP_Query($args); 
+0

Era in una situazione simile in cui ho optato per questa soluzione e ha funzionato perfettamente :). Grazie amico ha reso la mia giornata .. – Raajen

Problemi correlati