WP_Query OR-Klausel für tax_query und Schlüsselwörter
-
-
[bearbeiten] Sie Ihre Frage undteilen Siemit,was Siebereitsgetan haben.Niemand wirdeifrig auf Ihr leeres Blatt Papier zeichnen wollen,aber Siemüsseninitiieren.[edit] your Question and share what you've already done. Nobody will eager to draw on your blank sheet of paper, but you have to initiate.
- 0
- 2014-12-31
- Mayeenul Islam
-
@ MayeenulIslam Meine Frageistganz klar;Erhalten Sie Beiträge,dieentweder dem Kriterium "tax_query" oder "s"entsprechen.@MayeenulIslam My question is quite clear; get posts that match either the `tax_query` or the `s` criterion.
- 0
- 2014-12-31
- tyteen4a03
-
1 Antworten
- Stimmen
-
- 2015-01-07
Hieristein kleines Experiment:
Sie können dasfolgende Setup ausprobieren:
$args = array( 'wpse_search_or_tax_query' => true, // <-- New parameter! 's' => 'some search text', 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => array( 'some-category-slug' ), 'operator' => 'IN', ), ), ); $query = new WP_Query( $args );
Hierführen wir denbenutzerdefinierten Parameter
wpse_search_or_tax_query
ein,um die Abfrageänderungen zu aktivieren.Dies wird vomfolgenden Demo-Plugin unterstützt:
<?php /** * Plugin Name: Modify the WP_Query to use OR between the search and tax query parts. * Description: Activation through the boolean 'wpse_search_or_tax_query' parameter. * Plugin URI: http://wordpress.stackexchange.com/a/174221/26350 * Author: Birgir Erlendsson (birgire) * Version: 0.0.2 */ add_action( 'init', function() { if( ! is_admin() && class_exists( 'WPSE_Modify_Query' ) ) { $o = new WPSE_Modify_Query; $o->activate(); } }); class WPSE_Modify_Query { private $search = ''; public function activate() { add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); } public function pre_get_posts( WP_Query $q ) { if( filter_var( $q->get( 'wpse_search_or_tax_query' ), FILTER_VALIDATE_BOOLEAN ) && $q->get( 'tax_query' ) && $q->get( 's' ) ) { add_filter( 'posts_clauses', array( $this, 'posts_clauses' ), 10, 2 ); add_filter( 'posts_search', array( $this, 'posts_search' ), 10, 2 ); } } public function posts_clauses( $clauses, \WP_Query $q ) { remove_filter( current_filter(), array( $this, __FUNCTION__ ) ); // Generate the tax query: $tq = new WP_Tax_Query( $q->query_vars['tax_query'] ); // Get the generated taxonomy clauses: global $wpdb; $tc = $tq->get_sql( $wpdb->posts, 'ID' ); // Remove the search part: $clauses['where'] = str_ireplace( $this->search, ' ', $clauses['where'] ); // Remove the taxonomy part: $clauses['where'] = str_ireplace( $tc['where'], ' ', $clauses['where'] ); // Add the search OR taxonomy part: $clauses['where'] .= sprintf( " AND ( ( 1=1 %s ) OR ( 1=1 %s ) ) ", $tc['where'], $this->search ); return $clauses; } public function posts_search( $search, \WP_Query $q ) { remove_filter( current_filter(), array( $this, __FUNCTION__ ) ); $this->search = $search; return $search; } } // end class
Hoffentlich können Sie dies weiter an Ihre Bedürfnisse anpassen.
Here's a little experiment:
You can try the following setup:
$args = array( 'wpse_search_or_tax_query' => true, // <-- New parameter! 's' => 'some search text', 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => array( 'some-category-slug' ), 'operator' => 'IN', ), ), ); $query = new WP_Query( $args );
where we introduce the custom
wpse_search_or_tax_query
parameter to activate the query modifications.This is supported by the following demo plugin:
<?php /** * Plugin Name: Modify the WP_Query to use OR between the search and tax query parts. * Description: Activation through the boolean 'wpse_search_or_tax_query' parameter. * Plugin URI: http://wordpress.stackexchange.com/a/174221/26350 * Author: Birgir Erlendsson (birgire) * Version: 0.0.2 */ add_action( 'init', function() { if( ! is_admin() && class_exists( 'WPSE_Modify_Query' ) ) { $o = new WPSE_Modify_Query; $o->activate(); } }); class WPSE_Modify_Query { private $search = ''; public function activate() { add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); } public function pre_get_posts( WP_Query $q ) { if( filter_var( $q->get( 'wpse_search_or_tax_query' ), FILTER_VALIDATE_BOOLEAN ) && $q->get( 'tax_query' ) && $q->get( 's' ) ) { add_filter( 'posts_clauses', array( $this, 'posts_clauses' ), 10, 2 ); add_filter( 'posts_search', array( $this, 'posts_search' ), 10, 2 ); } } public function posts_clauses( $clauses, \WP_Query $q ) { remove_filter( current_filter(), array( $this, __FUNCTION__ ) ); // Generate the tax query: $tq = new WP_Tax_Query( $q->query_vars['tax_query'] ); // Get the generated taxonomy clauses: global $wpdb; $tc = $tq->get_sql( $wpdb->posts, 'ID' ); // Remove the search part: $clauses['where'] = str_ireplace( $this->search, ' ', $clauses['where'] ); // Remove the taxonomy part: $clauses['where'] = str_ireplace( $tc['where'], ' ', $clauses['where'] ); // Add the search OR taxonomy part: $clauses['where'] .= sprintf( " AND ( ( 1=1 %s ) OR ( 1=1 %s ) ) ", $tc['where'], $this->search ); return $clauses; } public function posts_search( $search, \WP_Query $q ) { remove_filter( current_filter(), array( $this, __FUNCTION__ ) ); $this->search = $search; return $search; } } // end class
Hopefully you can adjust this further to your needs.
-
Am Ende habeichmeinen eigenen Abfrage-Buildergeschrieben,aber hoffentlich hilft diesjemand anderem.I ended up writing my own query builder but hopefully this'll help somebody else.
- 1
- 2015-01-08
- tyteen4a03
-
Hallo,hast duesgetestet?es scheintnicht zufunktionieren,werfen Sie die Fehlerzeile 78 `Catchablefatalerror: Argument 2,das an WPSE_Modify_Query ::posts_search () übergeben wurde,musseine Instanz von WP_Query sein`.Also habeich die Parameterentfernt,aberesmachtimmernochnicht das ODER-Ding ...Hello, did you test it ? it seems not to be working, throw error line 78 `Catchable fatal error: Argument 2 passed to WPSE_Modify_Query::posts_search() must be an instance of WP_Query`. So I removed the params, but it still does not make the OR thing...
- 0
- 2015-10-02
- Vincent Wasteels
-
danke @VincentWasteels,ich habe den Code aktualisiert und hoffe,dasserjetzt wieerwartetfunktioniert ;-)thanks @VincentWasteels, I updated the code so hopefully it works now as expected ;-)
- 0
- 2015-10-02
- birgire
-
okmein schlechtes,ich habeesmit `get_posts` anstelle von` WP_Query ()` verwendet.Ichbin sehr überrascht,dass das Verhalten andersist ...ok my bad, I was using it with `get_posts`instead of `WP_Query()`. I'm very surprised the behaviour is different thought...
- 0
- 2015-10-02
- Vincent Wasteels
-
neinnein dein Code warendlich ok :)no no your code was ok finally :)
- 0
- 2015-10-02
- Vincent Wasteels
-
Ichbin froh zu hören,dassesfür dichgeklappt hat ;-) @VincentWasteelsglad to hear it worked out for you ;-) @VincentWasteels
- 0
- 2015-10-02
- birgire
Istesmöglich,eine ODER-Klausel zwischen
tax_query
und Schlüsselwörtern zuerstellen?Ichmöchte,dassWP_Query
Beiträge zurückgibt,wenneinetax_query
-Match odereines
-Schlüsselwortübereinstimmung vorliegt.