Guest User

Untitled

a guest
Dec 18th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. function extend_admin_search( $query ) {
  2. // Extend search for document post type
  3. if( ! is_admin() )
  4. return;
  5.  
  6. $post_type = 'post';
  7. // Custom fields to search for
  8. $custom_fields = array(
  9. "custom_field_1",
  10. "custom_field_2"
  11. );
  12.  
  13.  
  14. if ( $query->query['post_type'] != $post_type )
  15. return;
  16.  
  17. $search_term = $query->query_vars['s'];
  18.  
  19. //Set to empty, otherwise it won't find anything
  20. $query->query_vars['s'] = '';
  21.  
  22. if ( $search_term != '' ) {
  23. $meta_query = array( 'relation' => 'OR' );
  24.  
  25. foreach( $custom_fields as $custom_field ) {
  26. array_push( $meta_query, array(
  27. 'key' => $custom_field,
  28. 'value' => $search_term,
  29. 'compare' => 'LIKE',
  30. ));
  31. }
  32.  
  33. $query->set( 'meta_query', $meta_query );
  34. };
  35. }
  36.  
  37. add_action( 'pre_get_posts', 'extend_admin_search');
Add Comment
Please, Sign In to add comment