Advertisement
krot

wp search

Feb 4th, 2016
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1.         add_filter( 'posts_search', function( $search ) {
  2.         if($search) $search.=" and post_type='product'";//where поиска
  3.             return $search;
  4.         });
  5.         add_filter( 'posts_orderby', function( $orderby ) {//order by
  6.          $orderby="(CASE WHEN wp_posts.post_title LIKE '%".get_search_query()."%' THEN 1 ELSE 99 END)";
  7.             return $orderby;
  8.         });
  9.  
  10. get_search_query();//строка поиска
  11.  
  12. add_action( 'pre_get_posts', function( $q ) {
  13.     if( $q->is_main_query() && $q->is_search() )
  14.     {
  15.         $q->set( 'nopaging', true );
  16.         add_filter( 'posts_request', function( $request ) {//sql запрос поиска
  17.             $sql = 'YOUR EXTRA SQL QUERY';
  18.             return "({$request}) UNION ({$sql}) ORDER BY post_status DESC";
  19.         });  
  20.     }
  21. });
  22. where we use the no paging version to match your SQL query.
  23.  
  24.  
  25. /**
  26.  * Add data to the native WordPress search
  27.  *
  28.  * @see http://wordpress.stackexchange.com/a/161949/26350
  29.  */
  30. add_action( 'pre_get_posts', function( $q ) {
  31.     if( $q->is_main_query() && $q->is_search() )
  32.     {
  33.         $q->set( 'nopaging', true );
  34.         $c = new WPSE_Add_Search_Data;
  35.         $c->init();
  36.     }
  37. });
  38.  
  39. /**
  40.  * class WPSE_Add_Search_Data
  41.  */
  42. class WPSE_Add_Search_Data
  43. {
  44.     protected $search_where = '';
  45.  
  46.     public function init()
  47.     {
  48.         add_filter( 'posts_search',  array( $this, 'posts_search'  ) );
  49.         add_filter( 'posts_fields',  array( $this, 'posts_fields'  ) );
  50.         add_filter( 'posts_request', array( $this, 'posts_request' ) );
  51.         add_filter( 'posts_orderby', '__return_null' );
  52.     }
  53.  
  54.     public function posts_search( $search )
  55.     {
  56.         $this->search_where = $search;
  57.         return $search;
  58.     }
  59.  
  60.     public function posts_fields( $fields )
  61.     {
  62.         return 'ID, post_title, post_content, post_status';
  63.     }
  64.  
  65.     public function posts_request( $request )
  66.     {
  67.         global $wpdb;
  68.  
  69.         $search_where = str_ireplace(
  70.             array( $wpdb->posts . ".post_", "AND (password = '')" ),
  71.             "",
  72.             $this->search_where
  73.         );
  74.  
  75.         // Fetch data from the external table.
  76.         // The fields much match the fields from the main search query.
  77.         $sql = "SELECT ID, title as post_title,
  78.                       content as post_content, status as post_status
  79.                FROM {$wpdb->prefix}software_and_hardware_post
  80.                WHERE 1=1 {$search_where} AND status=1";
  81.  
  82.         // Append the external data with custom order:
  83.         return  "({$request}) UNION ({$sql}) ORDER BY post_status DESC";
  84.     }
  85.  
  86. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement