Il existe un moyen d'utiliser $ query-> set ('tax_query' dans le filtre pre_get_posts?
-
-
Pourquoipassez-vous un objet `WP_Query` dans uneméthode de définition d'argument?Why are you passing a `WP_Query` object into an argument setting method?
- 1
- 2011-12-01
- t31os
-
Oui,je me suistrompé,maintenantje l'ai obtenuen utilisantevalpour convertir la chaîneen untableau (etje suis vraiment sûr que la chaîneest sûre).Merci.Yes i was wrong, now i got it using eval to convert the string into an array (and im really sure that the string is safe). Thanks.
- 0
- 2011-12-01
- José Pablo Orozco Marín
-
2 réponses
- votes
-
- 2011-12-01
La variable
$query
dans lefiltre représente un objetWP_Query
,vousne devriez doncpaspasser unnouvel objetWP_Query
dans laméthodepour définir lespropriétés de cet objet.La question sur laquelle vous avez copié le code de n'utilisaitpas correctement lefiltre,ce qui,selonmoi,est au cœur de votreproblème.
Oui,
tax_query
peut être utilisé dans unfiltre/actionpre_get_posts
(ou similaireparse_request
).Voici unexemple:
Spécifiez unetaxonomiepersonnaliséepour les requêtes de recherchefunction search_filter_get_posts($query) { if ( !$query->is_search ) return $query; $taxquery = array( array( 'taxonomy' => 'career_event_type', 'field' => 'id', 'terms' => array( 52 ), 'operator'=> 'NOT IN' ) ); $query->set( 'tax_query', $taxquery ); } add_action( 'pre_get_posts', 'search_filter_get_posts' );
The
$query
variable in the filter represents aWP_Query
object, so you shouldn't be passing a newWP_Query
object into the method for setting that object's properties.The question you copied code from was incorrectly using the filter, which i feel is the crux of your issue.
Yes,
tax_query
can be used inside apre_get_posts
(or similarlyparse_request
) filter/action.Here is an example:
Specify a custom taxonomy for search queriesfunction search_filter_get_posts($query) { if ( !$query->is_search ) return $query; $taxquery = array( array( 'taxonomy' => 'career_event_type', 'field' => 'id', 'terms' => array( 52 ), 'operator'=> 'NOT IN' ) ); $query->set( 'tax_query', $taxquery ); } add_action( 'pre_get_posts', 'search_filter_get_posts' );
-
pourriez-vous donner unexemplepratique de définition de la requêtetax_query dans une actionpre_get_posts?could you give a working example of setting the tax_query inside a pre_get_posts action?
- 6
- 2012-02-22
- helgatheviking
-
$tax_queryest un objet qui contient untableauimbriqué.Vousne pouvezpas remplacer l'objetpar untableauimbriqué.$tax_query is an object that contains a nested array. You can't override the object with a nested array.
- 0
- 2013-04-17
- AlxVallejo
-
`$tax_query`n'estpas un objet,mais` $ query` l'est (c'est uneinstance de `WP_Query`).`$tax_query` is not an object, `$query` is though(it's an instance of `WP_Query`).
- 3
- 2013-04-17
- t31os
-
celane remplace-t-ilpas complètement latax_query?$taxqueryne devrait-ilpas être ajouté aux données actuelles dans l'argumenttax_query?doesn't this completely override the tax_query? shouldn't $taxquery be appended to the current data in the tax_query arg?
- 2
- 2018-05-25
- hot_barbara
-
@hot_barbaratel quel,cela écraserait latax_query.Cette version ajouterait à laplace la requête actuelle: $taxquery=tableau ( 'relation'=> 'OU', tableau ( 'taxonomy'=> 'type_événement_carrière', 'field'=> 'id', 'terms'=>tableau (52), 'operator'=> 'PAS IN' ) );@hot_barbara as it stands, it would overwrite the tax_query. This version would append the current query instead: $taxquery = array( 'relation' => 'OR', array( 'taxonomy' => 'career_event_type', 'field' => 'id', 'terms' => array( 52 ), 'operator'=> 'NOT IN' ) );
- 0
- 2020-01-20
- rambillo
-
- 2013-05-02
Les requêtesfiscalesnécessitent que vous définissiez également l'objettax_query dans la requête car la requête a déjà été analysée.Voirma réponsepour Modifier lespages detaxonomieenexclure les éléments destaxonomiesenfants .
Tax queries require you to also set the tax_query object in the query since the query has already been parsed. See my answer for Modify Taxonomy pages to exclude items in child taxonomies.
Ilexiste unmoyen d'utiliser
$query->set('tax_query', ...)
dans lefiltrepre_get_posts
? Parexemple,le code suivantne modifiepas la requête. Notez queje crée des $taxonomies àpartir d'une recherchepersonnalisée.Merci d'avance.