I am trying the get the data of an audio, I've uploaded to a Wordpress site I'm working on (Not my own). I'm quite new to WordPress, but as far as I know, media files such as images and audio is stored in wp_posts
alongside with all the regular posts but with a different post_type
.
On my site I'm using a theme called Soundtheme, which contains a playlist in which you can add songs. But the playlist works a bit odd, because when a song is added, it's only the title and the url to the audio file which is stored. So in fact it has no direct link to the actual uploaded file, except the url.
So to get the data, I tried to build a custom query, which would use the url from the songs in the playlist, to select all data in that row, where that url was matching. But some how I didn't get any result at all. And the strangest part was, that when I tried to use and uploaded image file instead it worked perfectly.
I know it sounds a bit complicated, but I just can't figure out, what else I should do.
Below is the code, I've tested with:
This one works perfectly (image file):
$file = 'http://example.com/wp-content/uploads/2016/05/image.jpg';
$thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE guid = '$file'" ) );
$theID = $thepost->ID;
$title = $thepost->post_title;
But this doesn't (exact same code, but with an audio file instead):
$file = 'http://example.com/wp-content/uploads/2016/05/music.mp3';
$thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE guid = '$file'" ) );
$theID = $thepost->ID;
$title = $thepost->post_title;
Can someone please help me, and tell me what could be wrong? And is there a better way to do all this?
Thanks
Guid
is an awful value to query with, it's not even idexed and database has to look for every single row. Posts, attachments, menus and pages share the same table, it can be very expensive at the end of the day. Why notID
?