Advertisement
Veerle

adaptation of the media search plugin to work within the med

Jan 9th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. <?php
  2. /** TEST
  3. * http://wordpress.org/support/topic/media-search
  4. * adaptation of the media search plugin to work within the media popup
  5. *
  6. * Plugin Name: Media Search
  7. * Description: Search media by attached page
  8. * Version: ***
  9. * Author: Valera Satsura
  10. * Author URI: http://satsura.com
  11. */
  12.  
  13. /**
  14. * Rewrite search for media library in admin area
  15. * @param $request
  16. * @return string
  17. */
  18. function media_by_page_search( $request ) {
  19. global $pagenow, $wpdb;
  20.  
  21. if ( is_admin() ) {
  22. $searchstring = NULL;
  23. $paged = 1;
  24. $post_mime_type = NULL;
  25.  
  26. // check if this is the media popup (ajax call POST)
  27. if ( isset($_POST['action']) && $_POST['action'] == 'query-attachments' ) {
  28. if (isset($_POST['query'])) {
  29. $post_query = $_POST['query'];
  30. $searchstring = $post_query['s'];
  31. $post_mime_type = $post_query['post_mime_type'];
  32. $paged = $post_query['paged'];
  33. }
  34. }
  35.  
  36. // check if this is the media library page (GET)
  37. if ( $pagenow == 'upload.php' && isset($_GET['s']) && !empty($_GET['s']) ) {
  38. $searchstring = $_GET['s'];
  39. if (isset($_GET['post_mime_type']))
  40. $post_mime_type = $_GET['post_mime_type'];
  41. $paged = get_query_var( 'paged' );
  42. }
  43.  
  44. // That search works only on "Media Library"
  45. if ( isset($searchstring) && $searchstring != '' ) {
  46.  
  47. // Get page limits
  48. $media_per_page = (int) get_user_option( 'upload_per_page' );
  49. if ( empty( $media_per_page ) || $media_per_page < 1 )
  50. $media_per_page = 40;
  51. $media_per_page = apply_filters( 'upload_per_page', $media_per_page );
  52.  
  53. // Start page, $paged = current page number
  54. $start = (int) $media_per_page * (int) ( (int) $paged - 1 );
  55.  
  56. // SQL to search
  57.  
  58. // wp_posts > post_title = img title
  59. // wp_posts > post_content = img description
  60. // wp_posts > post_excerpt = img caption
  61. // wp_postmeta > _wp_attachment_image_alt = img alt txt
  62. // wp_postmeta > _wp_attached_file = path
  63. $request = "SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts
  64. WHERE 1=1
  65. AND
  66. (
  67. (
  68. (wp_posts.post_title LIKE '%" . $searchstring . "%')
  69. OR (wp_posts.post_content LIKE '%" . $searchstring . "%')
  70. OR (wp_posts.post_excerpt LIKE '%" . $searchstring . "%')
  71. )
  72. OR
  73. (
  74. ID IN
  75. (
  76. select post_id from $wpdb->postmeta as b where b.meta_key = ('_wp_attachment_image_alt' or '_wp_attached_file') and b.meta_value like '%" . $searchstring . "%'
  77. )
  78. )
  79. OR
  80. (
  81. post_parent IN
  82. (
  83. select ID from $wpdb->posts as c where c.post_type <> 'attachment' and c.post_title like '%" . $searchstring . "%'
  84. )
  85. )
  86. )
  87. AND
  88. wp_posts.post_type = 'attachment'
  89. AND
  90. (
  91. wp_posts.post_status = 'inherit' OR wp_posts.post_status = 'private'
  92. )";
  93.  
  94. if ($post_mime_type && $searchstring != '')
  95. $request .= " AND
  96. (
  97. wp_posts.post_mime_type like '%" . $post_mime_type . "%'
  98. )";
  99.  
  100.  
  101. $request .= " ORDER BY wp_posts.post_date DESC LIMIT " . (int)$start . ", " . (int)$media_per_page;
  102.  
  103. }
  104. }
  105. return $request;
  106. }
  107.  
  108. // Register new search
  109. add_filter( 'posts_request' , 'media_by_page_search' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement