Guest User

Untitled

a guest
Apr 26th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. <?php
  2.  
  3. add_action( 'pre_get_posts', function( $query ) {
  4. if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
  5. return
  6. }
  7.  
  8. if ( is_admin() || ! isset( $query->query['post_type'] ) || $query->query['post_type'] !== 'product' ) {
  9. return;
  10. }
  11.  
  12. if ( isset( $_GET['search'] ) && ! empty( $_GET['search'] ) && class_exists( '\SWP_Query' ) ) {
  13. /** Let's build a SWP_Query and get some search results */
  14. $args = [
  15. 's' => $_GET['search'],
  16. 'fields' => 'ids',
  17. 'posts_per_page' => $query->get( 'posts_per_page' ),
  18. ];
  19.  
  20. if ( ! empty( $query->get( 'tax_query' ) ) ) {
  21. $args['tax_query'] = $query->get( 'tax_query' );
  22. }
  23.  
  24. if ( ! empty( $query->get( 'meta_query' ) ) ) {
  25. $args['meta_query'] = $query->get( 'meta_query' );
  26. }
  27.  
  28. /** Now we take our search results and punch them into the query */
  29. $swp = new \SWP_Query( $args );
  30. if ( ! empty( $swp->posts ) ) {
  31. $query->set( 'post__in', $swp->posts );
  32. // Tell the query it's not a search anymore or we get screwed up results
  33. $query->set( 's', '' );
  34. }
  35. }
  36.  
  37. }, 1, 1 );
Add Comment
Please, Sign In to add comment