Ottenere i Termini per ID con ordine degli ID

2 feb 2017, 21:29:19
Visualizzazioni: 20.1K
Voti: 6

Voglio ottenere i Termini per ID mantenendo l'ordine degli ID. Ma non funziona, WordPress cambia automaticamente l'ordine.

Il mio codice-

$catsArray = array(159, 155, 143, 153, ......);
$series = get_terms( array(
              'taxonomy' => 'ctc_sermon_series',
              'number'  =>  9,
              'offset'  =>  $offset,
              'include' => $catsArray,
              'hide_empty'  => false, 
            ) );

E il risultato-

(
    [0] => WP_Term Object
        (
            [term_id] => 155
            [name] => 10
            [slug] => 10
            [term_group] => 0
            [term_taxonomy_id] => 155
            [taxonomy] => ctc_sermon_series
            [description] => 
            [parent] => 0
            [count] => 1
            [filter] => raw
        )

    [1] => WP_Term Object
        (
            [term_id] => 159
            [name] => 14
            [slug] => 14
            [term_group] => 0
            [term_taxonomy_id] => 159
            [taxonomy] => ctc_sermon_series
            [description] => 
            [parent] => 0
            [count] => 1
            [filter] => raw
        )

    [2] => WP_Term Object
        (
            [term_id] => 153
            [name] => Nome 8
            [slug] => name-8
            [term_group] => 0
            [term_taxonomy_id] => 153
            [taxonomy] => ctc_sermon_series
            [description] => Nome 8 Des
            [parent] => 0
            [count] => 1
            [filter] => raw
        )

    [3] => WP_Term Object
        (
            [term_id] => 143
            [name] => Serie 1
            [slug] => series-1
            [term_group] => 0
            [term_taxonomy_id] => 143
            [taxonomy] => ctc_sermon_series
            [description] => Descrizione Serie 1
            [parent] => 0
            [count] => 3
            [filter] => raw
        )

)

Mi aspetto di ottenere prima il termine con ID-159, ma ogni volta ottengo prima il risultato dell'ID-155. Ho bisogno di ottenere il risultato secondo la sequenza dell'array degli ID.

Grazie in anticipo :)

0
Tutte le risposte alla domanda 3
0
17

Quindi credo che la domanda sia come ottenere i termini nell'ordine degli ID forniti - che potrebbero non essere ordinati in modo crescente o decrescente, ma in un ordine casuale.

Sorprendentemente, penso che ci sia una scorciatoia per questo in WP - chi lo avrebbe mai detto? Questo, credo, è ciò che vuoi usare:

$catsArray = array(159, 155, 143, 153, ......);
$series = get_terms( array(
          'taxonomy' => 'ctc_sermon_series',
          'number'  =>  9,
          'offset'  =>  $offset,
          'include' => $catsArray,
          'hide_empty'  => false, 
          'orderby'  => 'include', // <--- 
        ) );

Spero che questo sia d'aiuto!

2 feb 2017 23:01:21
2

Questo potrebbe funzionare per te, anche se non l'ho testato.

$catsArray = array(159, 155, 143, 153, ......);
$series = get_terms( array(
              'taxonomy' => 'ctc_sermon_series',
              'number'  =>  9,
              'offset'  =>  $offset,
              'include' => $catsArray,
              'hide_empty'  => false, 
              'orderby' => 'term_id',
              'order' => 'DESC', // oppure ASC
            ) );
2 feb 2017 22:49:46
Commenti

In realtà voglio ottenere i termini secondo l'ordine effettivo dell'array, non per ordine di term_id.

Arif Khan Arif Khan
2 feb 2017 22:57:58

capito. La risposta di @MacPrawn è ciò che stai cercando. ho votato positivamente

Anwer AR Anwer AR
2 feb 2017 23:17:41
0

Basta utilizzare il parametro 'fields'.

wp_get_post_terms($post_id, 'some_taxonomy', array('fields' => 'ids'))

Leggi questo https://wp-kama.com/function/wp_get_post_terms

13 set 2022 10:27:47