Ordinare o Orderby in tax_query (Come definire l'ordine dei termini in WP_Query)
Sto cercando di capire come posso definire l'ordine dei termini nella mia WP_QUERY.
Sto creando una knowledge base. Ho un CPT chiamato knowledge-base e una tassonomia chiamata topics.
Il mio codice mostra tutti i termini della tassonomia e poi cicla attraverso 4 post che hanno la meta key top_four_num, per mostrare le prime quattro domande nell'argomento. Poi ho un loop annidato per i restanti post in quell'argomento. Questi sono nascosti da JQUERY e l'utente può cliccare su un link 'mostra tutto' per visualizzarli.
Tutto funziona come desidero, ma non riesco a capire come cambiare l'ordine dei termini. Attualmente appaiono in ordine alfabetico.
Ho controllato il codex e tax_query non ha parametri basati sull'ordine. Qualcuno può indicarmi la direzione giusta per poter controllare l'ordine di tax_query?
Il mio codice è qui sotto. Qualsiasi aiuto è molto apprezzato!
<?php
/**
* Knowledge Base
*/
//Includi l'header
get_header();
//* Personalizza il testo della casella di ricerca
add_filter( 'genesis_search_text', 'sp_search_text' );
function sp_search_text( $text ) {
return esc_attr( 'Facci una domanda...' );
}
// Rimuovi il div.wrap da div.site-inner
add_filter( 'genesis_structural_wrap-site-inner', '__return_empty_string' );
//* Aggiungi una classe personalizzata al body
add_filter( 'body_class', 'sp_body_class' );
function sp_body_class( $classes ) {
$classes[] = 'KB';
return $classes;
} ?>
<style>.site-inner {
max-width: none!important;
padding-top: 0;
} </style>
<!-- Banner Superiore -->
<div style = "text-align: center; " class = "top-banner">
<h1 style = "color: white;">Mostra della knowledge base</h1>
<?php get_search_form(); ?>
</div>
<!--classe body-->
<div class = "body-kb">
<?php
// Inizia il loop principale degli argomenti
$_terms = get_terms( array('topic') );
foreach ($_terms as $term) :
$ids = get_field('top_four', false, false);
$term_slug = $term->slug;
$_first = new WP_Query( array(
'post_type' => 'knowledge-base',
'order' => 'ASC',
'orderby' => 'meta_value_num',
'meta_key' => 'top_four_num',
'posts_per_page' => -4,
'tax_query' => array(
array(
'taxonomy' => 'topic',
'field' => 'slug',
'terms' => $term_slug,
),
),
));
if( $_first->have_posts() ) :
?> <div class = "container1"><?php
//Mostra il nome del termine
echo '<div class = "term-name"><h2>'. $term->name .'</h2></div>';
echo '<hr id="short-line" align="left" width="100">';
while ( $_first->have_posts() ) : $_first->the_post();
?>
<?php
//Contatore del loop per aggiungere div ai primi post restituiti
if( 0 == $_first->current_post ) {
?> <div class = "example-left"> <?php
}?>
<?php if( 2 == $_first->current_post ) {
?> </div> <?php
}?>
<?php if( 2 == $_first->current_post ) {
?> <div class = "example-right"> <?php
}?>
<?php // mostra le informazioni del post ?>
<div class="answers">
<h4><a href = "<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<p><?php get_post_meta( get_the_ID(), 'url', true ); ?></p>
</div>
<?php
endwhile;?>
<?php // Inizia il loop secondario del contenuto nascosto ?>
<?php $term_slug = $term->slug;
$_second = new WP_Query( array(
'post_type' => 'knowledge-base',
'order' => 'ASC',
'posts_per_page' => 10, //importante per evitare warning sul limite di memoria PHP
/* 'offset' => 5*/
'meta_key' => 'priority',
'meta_value' => '1',
'meta_compare' => '>=',
'tax_query' => array(
array(
'taxonomy' => 'topic',
'field' => 'slug',
'terms' => $term_slug,
),
),
));
if( $_second->have_posts() ) :
?>
</div>
<!---PULSANTI NASCONDI/MOSTRA JQUERY-->
<div class = "view-more"><a class="arrow-link" data-expand="howitworks-expand">Mostra altro<img class="main-arrow" src="/wp-content/uploads/arrow@2x-1.png" alt="Freccia mostra altro" title="Mostra altro contenuto"></a></div>
<div class = "show-content"><?php
$i = 0;
while ( $_second->have_posts() ) : $_second->the_post();
?>
<?php //Mostra il loop nascosto in due colonne automaticamente
if ($i == 0) echo '<div class="one-half first">';
if ($i == (round($_second->post_count / 2))) echo '</div><div class="one-half">';
?> <p class = "rest-of-p sub"><a href = "<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<p><?php get_post_meta( get_the_ID(), 'url', true ); ?></p><?php
if ($i == round($_second->post_count)) echo '</div>';
$i++;
?>
<?php endwhile;?>
<?PHP wp_reset_postdata(); ?>
</div>
<?php endif;?>
<?PHP //FINE LOOP 2 ?>
<!--chiudi container-->
</div>
</div><?php
endif;
endforeach;
?>
</div>
<?php
// Includi il footer
get_footer();
?>
Dalla documentazione di get_terms
:
orderby (string)
: Campo(i) per ordinare i termini. Accetta campi dei termini ('name', 'slug', 'term_group', 'term_id', 'id', 'description'), 'count' per il conteggio della tassonomia, 'include' per corrispondere all'ordine del parametro $include, 'meta_value', 'meta_value_num', il valore di $meta_key, le chiavi dell'array $meta_query, o 'none' per omettere la clausola ORDER BY. Il valore predefinito è 'name'.
Esempio:
get_terms([
'taxonomy' => 'topic',
'orderby' => 'field',
'order' => 'ASC/DESC',
]);
