Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action( 'pre_get_posts', function( $query ) {
- if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
- return
- }
- if ( is_admin() || ! isset( $query->query['post_type'] ) || $query->query['post_type'] !== 'product' ) {
- return;
- }
- if ( isset( $_GET['search'] ) && ! empty( $_GET['search'] ) && class_exists( '\SWP_Query' ) ) {
- /** Let's build a SWP_Query and get some search results */
- $args = [
- 's' => $_GET['search'],
- 'fields' => 'ids',
- 'posts_per_page' => $query->get( 'posts_per_page' ),
- ];
- if ( ! empty( $query->get( 'tax_query' ) ) ) {
- $args['tax_query'] = $query->get( 'tax_query' );
- }
- if ( ! empty( $query->get( 'meta_query' ) ) ) {
- $args['meta_query'] = $query->get( 'meta_query' );
- }
- /** Now we take our search results and punch them into the query */
- $swp = new \SWP_Query( $args );
- if ( ! empty( $swp->posts ) ) {
- $query->set( 'post__in', $swp->posts );
- // Tell the query it's not a search anymore or we get screwed up results
- $query->set( 's', '' );
- }
- }
- }, 1, 1 );
Add Comment
Please, Sign In to add comment