rtMedia is widely known plugin for WordPress and BuddyPress media management. It is a very rich featured plugin, nicely integrated with BuddyPress. One of the main feature of rtMedia for BuddyPress, It provides ‘Media’ tab for member’s profile, from where members can upload and show images, videos and other types of media.

Sometimes you might want to sort those media showed in user’s media tab. rtMedia has already few default sorting feature, as example sort by Like count, Comment Count, But if you want to go further and want to sort specifically my media ID, there is no direct solution out there. So this post is about that small trick.

So let’s say you want to sort by media id (id column from rtMedia’s media table). You can follow those steps to do that…

Let’s write the function first which will be hooked later with rtMedia media model order query

function rtmedia_my_custom_media_order_query($order){
}
add_filter('rtmedia-model-order-by-query','rtmedia_my_custom_media_order_query');

 

Let’s fix the ID’s first in sequence that you want to sort

$media_id_sequence = '5,3,8,2,10';

or let’s say you don’t want this IDs hard-coded, instead, you can take from user meta

$user_id = bp_displayed_user_id();
$media_id_sequence = get_user_meta($user_id,'_rtmedia_custom_media_order', true);

 

Now let’s build mySQL Order BY query

$order = "ORDER BY FIELD(id,".$media_id_sequence.")";

So, the full function is,

function rtmedia_my_custom_media_order_query($order){
$user_id = bp_displayed_user_id();
// check usermeta for custom sequence with '_rtmedia_custom_media_order' meta key
$media_id_sequence = get_user_meta($user_id,'_rtmedia_custom_media_order', true);
// Return original order query if sequence is empty
if( empty($media_id_sequence) )
return $order;
// if sequence exists, then build custom order query and return
return "ORDER BY FIELD(id,".$media_id_sequence.")";
}
add_filter('rtmedia-model-order-by-query','rtmedia_my_custom_media_order_query');

Put this function to your theme’s functions.php file and then it will do the magic! Now all the media will sorted by given sequence 🙂