searchwp\post_stati
Desde: 4.0.0
Sumário
Controle os status de postagem considerados para Fontes baseadas em \WP_Post.
Observe que, para que este hook tenha o efeito adequado, você deve entender como o Índice do SearchWP funciona. O Índice representa o 'pool máximo' de resultados potenciais, e as Consultas são executadas contra esse Índice para limitar as entradas retornadas.
Dito isso, para personalizar quais status de postagem são retornados nos resultados, precisamos seguir um processo de duas etapas:
- Ampliar o Índice para conter todos os status de postagem aplicáveis
- Enganchar nas Consultas para garantir que apenas nossos status de postagem esperados sejam retornados
? Prossiga com cautela, pois o uso indevido deste hook pode expor dados!
Parâmetros
| Tipo | Parâmetro | Padrão | Desde | ||||||
|---|---|---|---|---|---|---|---|---|---|
| string[] | $post_stati |
Status de postagem que são públicos e não excluídos da pesquisa | 4.0.0 |
||||||
| Array |
$args
|
4.0.0 |
|||||||
Exemplos
All hooks should be added to your custom SearchWP Customizations Plugin.
Incluir rascunhos no Motor Suplementar
| <?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 ); |

