wpdb function for filtering the images by title

So firstly as Q Studio stated you should really have researched and tried to find out on your own first and post anything you have tried then we can help you on your coding journey.

But I don’t like to see someone not get any help at all.

Within your function you will want to make sure you do 3 things.

Number 1 call the global variable $wpdb

Number 2 you will want to find out where in the database your images are being stored. Learn about the $wpdb->get_results(); function.

Number 3 understand how to get data from a database based on the first letter, this can be done with the LIKE command within the sql and %A %B %C ect ect.

So lets tackle them 3 together shall we.

Number 1, to call the global variable $wpdb we just need to place the following code inside our function.

global $wpdb;

Number 2 and 3, In our database the images are stored in PREFIX_posts, with the prefix required for your database, most would be wp_posts but I changed mine on installation for security purposes, in the _posts file there is a column called post_type, the images are stored as attachments, then the actual file url is stored in the table guid, the name is saved as post_name. To use $wpdb to get the data we would use

$table = PREFIX_posts;
$row =  $wpdb->get_results("SELECT guid FROM $table WHERE post_type="attachment" AND post_name LIKE %a");

Now $row contains an array of each database entry of an attachment.

I would suggest looking at your database and also studying the post_mine_type column where you can decipher the difference between an image and video, I will leave that to you.

You will need to use a foreach loop to display each one

foreach ($row as $row) {
    echo 'Image Name: '.$row->post_title.' <br> Image: <img src="'.$row->guid.'"></img><br>';
}

Those are the very basics, I hope you find them of some use in creating your function and post.

Happy new year.

-Chalkie