1

Due to lack of reputation, I must now ask the question. I want to create a search for all images in the media libary. The search result should only appear if you search for the exact title of the image. I already tried it with 'exact' => true but it does not work. Still get similar images displayed. I have created a query with the following parameters:

<?php
$args = array(
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'orderby' => 'post_date',
        'order' => 'desc',
        'posts_per_page' => '30',
        'post_status'    => 'inherit',
         );

     $loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();

$image = wp_get_attachment_image_src( get_the_ID() );
echo "<img src='" . $image[0] . "'>";

endwhile; ?>

Hope that somebody can help me.

1 Answer 1

0

Try something like that

The reverse of this: https://wordpress.stackexchange.com/a/209714/180599

<?php
global $wpdb;

//The reverse of this: https://wordpress.stackexchange.com/a/209714/180599
$supported_mimes = array( 'image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon' );
$all_mimes = get_allowed_mime_types();
$accepted_mimes = array_diff($supported_mimes, $all_mimes);

//and...
$keyword = stripslashes($_REQUEST['keyword']);
//$search = $wpdb->get_col( $wpdb->prepare( " SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_title = '%s' OR post_content = '%s' ", $keyword, $keyword ) );
$search = $wpdb->get_col( $wpdb->prepare( " SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_title = '%s' ", $keyword ) );
$query = array(
    'post_type' => 'attachment', 
    'post_status' => 'inherit',
    'orderby' => 'date', 
    'order' => 'DESC',
    'posts_per_page' => 10,
    'post_mime_type' => $accepted_mimes,
    'post__in' => $search,
);  
$attachments = new WP_Query($query);
while($attachments->have_posts()):
    $attachments->the_post();

    echo $attachment->post_content.'<br>';
    echo $attachment->post_title.'<br>';
    //echo wp_get_attachment_image_src($attachment->ID, 'medium', true )[0].'<hr>';
    echo wp_get_attachment_url($attachment->ID).'<hr>';

endwhile;
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.