Utilizzo di pre_get_posts con WP_Query


24

Stavo leggendo l' eccellente risposta di Stephen Harris a questa domanda sull'uso di WP_query(), query_posts()e pre_get_posts.

Dice "pre_get_posts è un filtro per alterare qualsiasi query. Viene spesso usato per modificare solo la" query principale "."

È possibile utilizzare pre_get_postsper filtrare solo una specifica query secondaria creata con WP_Query? per esempio.

$my_secondary_loop = new WP_Query(...);
if( $my_secondary_loop->have_posts() ):
    while( $my_secondary_loop->have_posts() ): $my_secondary_loop->the_post();
       //The secondary loop
    endwhile;
endif;
wp_reset_postdata();

Qualsiasi aiuto molto apprezzato.

Risposte:


23

Il modo più semplice è aggiungere l'azione subito prima della query e rimuoverla immediatamente dopo.

add_action('pre_get_posts', 'some_function_in_functionsphp');
$my_secondary_loop = new WP_Query(...);
remove_action('pre_get_posts', 'some_function_in_functionsphp');

if( $my_secondary_loop->have_posts() ):
    while( $my_secondary_loop->have_posts() ): $my_secondary_loop->the_post();
       //The secondary loop
    endwhile;
endif;
wp_reset_postdata();

MODIFICARE

Un'altra tecnica che puoi usare è impostare la tua var di query e verificarla in un gancio:

// tell WordPress about our new query var
function wpse52480_query_vars( $query_vars ){
    $query_vars[] = 'my_special_query';
    return $query_vars;
}
add_filter( 'query_vars', 'wpse52480_query_vars' );

// check if our query var is set in any query
function wpse52480_pre_get_posts( $query ){
    if( isset( $query->query_vars['my_special_query'] ) )
        // do special stuff

    return $query;
}
add_action( 'pre_get_posts', 'wpse52480_pre_get_posts' );

e nel modello:

// set the query var (along with whatever others) to trigger the filter
$args = array(
    'my_special_query' => true
);
$my_secondary_loop = new WP_Query( $args );

Grazie mille Milo. Questo è davvero molto utile. Mi sono sempre chiesto se fosse possibile impostare la propria query var.
— Ben Pearson,

Come posso usare questo trucco per la pagina di archivio? Non voglio riscrivere di nuovo l'intera query per la pagina di archivio, ma voglio usare questa tecnica.
— Rohit Pande,

4

pre_get_posts viene generato per ogni query post:

  • get_posts ()
  • nuovo WP_Query ()
  • Quel post recente casuale widget il tuo client installato a tua insaputa.
  • Qualunque cosa

- @nacin

Detto questo, a meno che non si escluda il filtro, utilizzare il condizionale: is_main_query()il filtro verrà attivato sul nuovo WP_Query.

Se vuoi solo scegliere come target il tuo nuovo WP_Query specifico, non c'è modo di farlo.


E la tecnica di Milo? Non l'ho mai visto prima ...
— brasofilo,

La sua tecnica funzionerà. Non l'ho mai usato per pre_get_posts ma per altri filtri come posts_where
— Chris_O,

1
Bene, qualcosa di nuovo imparato oggi!
— brasofilo,
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.