Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /** TEST
- * http://wordpress.org/support/topic/media-search
- * adaptation of the media search plugin to work within the media popup
- *
- * Plugin Name: Media Search
- * Description: Search media by attached page
- * Version: ***
- * Author: Valera Satsura
- * Author URI: http://satsura.com
- */
- /**
- * Rewrite search for media library in admin area
- * @param $request
- * @return string
- */
- function media_by_page_search( $request ) {
- global $pagenow, $wpdb;
- if ( is_admin() ) {
- $searchstring = NULL;
- $paged = 1;
- $post_mime_type = NULL;
- // check if this is the media popup (ajax call POST)
- if ( isset($_POST['action']) && $_POST['action'] == 'query-attachments' ) {
- if (isset($_POST['query'])) {
- $post_query = $_POST['query'];
- $searchstring = $post_query['s'];
- $post_mime_type = $post_query['post_mime_type'];
- $paged = $post_query['paged'];
- }
- }
- // check if this is the media library page (GET)
- if ( $pagenow == 'upload.php' && isset($_GET['s']) && !empty($_GET['s']) ) {
- $searchstring = $_GET['s'];
- if (isset($_GET['post_mime_type']))
- $post_mime_type = $_GET['post_mime_type'];
- $paged = get_query_var( 'paged' );
- }
- // That search works only on "Media Library"
- if ( isset($searchstring) && $searchstring != '' ) {
- // Get page limits
- $media_per_page = (int) get_user_option( 'upload_per_page' );
- if ( empty( $media_per_page ) || $media_per_page < 1 )
- $media_per_page = 40;
- $media_per_page = apply_filters( 'upload_per_page', $media_per_page );
- // Start page, $paged = current page number
- $start = (int) $media_per_page * (int) ( (int) $paged - 1 );
- // SQL to search
- // wp_posts > post_title = img title
- // wp_posts > post_content = img description
- // wp_posts > post_excerpt = img caption
- // wp_postmeta > _wp_attachment_image_alt = img alt txt
- // wp_postmeta > _wp_attached_file = path
- $request = "SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts
- WHERE 1=1
- AND
- (
- (
- (wp_posts.post_title LIKE '%" . $searchstring . "%')
- OR (wp_posts.post_content LIKE '%" . $searchstring . "%')
- OR (wp_posts.post_excerpt LIKE '%" . $searchstring . "%')
- )
- OR
- (
- ID IN
- (
- 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 . "%'
- )
- )
- OR
- (
- post_parent IN
- (
- select ID from $wpdb->posts as c where c.post_type <> 'attachment' and c.post_title like '%" . $searchstring . "%'
- )
- )
- )
- AND
- wp_posts.post_type = 'attachment'
- AND
- (
- wp_posts.post_status = 'inherit' OR wp_posts.post_status = 'private'
- )";
- if ($post_mime_type && $searchstring != '')
- $request .= " AND
- (
- wp_posts.post_mime_type like '%" . $post_mime_type . "%'
- )";
- $request .= " ORDER BY wp_posts.post_date DESC LIMIT " . (int)$start . ", " . (int)$media_per_page;
- }
- }
- return $request;
- }
- // Register new search
- add_filter( 'posts_request' , 'media_by_page_search' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement