I want to display all my attachments(images) under tag if image file name has contain that tag (word) in file name.
I mean if I have tag http://example.com/tag/john
I want to display all images that have john in their name.
like /wp-content/uploads/2018/08/my_name-john.jpg, /wp-content/uploads/2017/06/john_meet-us.jpg,
Im new to php and also to wordpress, i dont know if that possible to fetch tag from url that open in browser and then find images for that tag. I know its helping form, i also have to provide codes what i try and then people help us. but as i said im new yes i google it and i try this code its work but have many limits and also dont know how to modifies for my use.
<?php
function get_attachment_url_by_title( $title ) {
$attachment = get_page_by_title($title, OBJECT, 'attachment');
//print_r($attachment);
if ( $attachment ){
$attachment_url = $attachment->guid;
}else{
return 'image-not-found';
}
return $attachment_url;
}
echo get_attachment_url_by_title('title of image');
?>
Above code show image if we manually provide title of the image, its its must same as file name then above code work otherwise not. so hope you got my question. Thanks in advance. ( Sorry for my bad English)
You can get the attachments url by tag by below Sql Query :
Paste the below code in your current active theme functions.php file.
function get_url_from_tag_name($tag = 'old'){
global $wpdb;
$query = 'SELECT guid FROM `'.$wpdb->prefix.'posts` WHERE guid like "%'.$tag.'%" AND post_type="attachment"';
$results = $wpdb->get_results( $query, OBJECT );
return $results;
}
I hope it help you. Thanks :)