0

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

  • 1
    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 not ID? – N00b May 12 '16 at 12:24
0

MP3 Playlists have a .m3u extension. I'm guessing that your Media Library is not registering the MP3s but the playlists instead, so they may physically be in that location, but not in the database.

One work around if you know the names of the files could be to try using the PHP id3_get_tag() function.

Read more here: http://php.net/manual/en/function.id3-get-tag.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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