searchwp\post_stati
Da: 4.0.0
Indice
Controlla gli stati dei post considerati per le sorgenti basate su \WP_Post.
Nota che affinché questo hook abbia effetto, devi comprendere come funziona l'indice di SearchWP. L'indice rappresenta il pool 'massimo' di risultati potenziali e le query vengono eseguite contro tale indice per limitare le voci restituite.
Detto questo, per personalizzare quali stati dei post vengono restituiti nei risultati, dobbiamo seguire un processo in due fasi:
- Ampliare l'indice per contenere tutti gli stati dei post applicabili
- Agganciarsi alle query per garantire che vengano restituiti solo i nostri stati dei post previsti
? Procedi con cautela poiché un uso improprio di questo hook può esporre dati!
Parametri
| Tipo | Parametro | Predefinito | Da | ||||||
|---|---|---|---|---|---|---|---|---|---|
| String[] | $post_stati |
Stati dei post pubblici e non esclusi dalla ricerca | 4.0.0 |
||||||
| Array |
$args
|
4.0.0 |
|||||||
Esempi
All hooks should be added to your custom SearchWP Customizations Plugin.
Includi bozze nel motore supplementare
| <?php | |
| /** | |
| * Include Drafts in SearchWP Supplemental Engine results. | |
| * | |
| * NOTE: In order for this to work we need to first tell SearchWP to index | |
| * ALL of the potential post stati. We can then curate which post stati | |
| * to consider during searches; it is a two-step process. | |
| */ | |
| // Step 1: tell SearchWP to index Drafts in addition to its default post stati. | |
| add_filter( 'searchwp\post_stati', function( $post_stati, $args ) { | |
| $post_stati[] = 'draft'; | |
| return $post_stati; | |
| }, 20, 2 ); | |
| // Step 2: limit post stati during searches, per post type. By default | |
| // SearchWP is going to respect the stati we defined in Step 1! | |
| add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
| // If this is the 'supplemental' Engine, search all post stati. | |
| if ( 'supplemental' === $query->get_engine()->get_name() ) { | |
| return $mods; | |
| } | |
| // Only return WP_Posts with 'publish' post status. | |
| foreach ( $query->get_engine()->get_sources() as $source ) { | |
| $flag = 'post' . SEARCHWP_SEPARATOR; | |
| if ( 'post.' !== substr( $source->get_name(), 0, strlen( $flag ) ) ) { | |
| continue; | |
| } | |
| $mod = new \SearchWP\Mod( $source ); | |
| $mod->set_where( [ [ | |
| 'column' => 'post_status', | |
| 'value' => [ 'publish' ], | |
| 'compare' => 'IN', | |
| ] ] ); | |
| $mods[] = $mod; | |
| } | |
| return $mods; | |
| }, 20, 2 ); |

