Custom code to display only particular media with rtmedia_gallery shortcode

Use this snippet to add the media_ids parameter to rtmedia_gallery shortcode in order to display any particular media you want.

Refer to rtmedia_gallery shortcode documentation page for more information.

/**
 * Magic begins from here
 */
function my_rtmedia_set_query_filters() {
    add_filter( 'rtmedia_media_query', 'my_modify_media_query', 9, 3 );
}
add_action( 'rtmedia_set_query', 'my_rtmedia_set_query_filters', 99 );

/**
 * Modifies the media query. It adds the filter to alter the WHERE parameter of the
 * MySQL query and removes the context_id and context if set ( by the rtMedia plugin )
 *
 * @param  array     $media_query      Refer the `rtmedia_media_query` filter defined in the
 *                                  rtMedia plugin
 * @param  array    $action_query
 * @param  array      $query
 *
 * @return array     $media_query
 */
function my_modify_media_query( $media_query, $action_query, $query ) {

    global $rtmedia_query, $media_query_clone_ids;

    // Store the `media_ids` parameter to be used in the rtmedia-model-where-query filter
    $media_query_clone_ids = $media_query;

    if ( isset( $media_query['media_ids'] ) && '' != $media_query['media_ids'] ) {

        // Add the filter to modify the where parameter
        add_filter( 'rtmedia-model-where-query', 'my_rtmedia_model_shortcode_where_query_attributes', 10, 3 );

        // unset it, so that it wont affect the other rtmedia_gallery shortcodes on the same page
        unset( $media_query['media_ids'] );

        // unset from global query so that multiple gallery shortcode can work
        if ( isset( $rtmedia_query->query ) && isset( $rtmedia_query->query['media_ids'] ) ) {
            unset( $rtmedia_query->query['media_ids'] );
        }

        if ( isset( $media_query['context_id'] ) ) {
            unset( $media_query['context_id'] );
        }

        if ( isset( $media_query['context'] ) ) {
            unset( $media_query['context'] );
        }
    }

    return $media_query;
}

/**
 * Modify the WHERE parameter
 *
 * For the parameter description refer the `rtmedia-model-where-query` filter defined in the
 * rtMedia plugin
 */
function my_rtmedia_model_shortcode_where_query_attributes( $where, $table_name, $join ) {
    global $rtmedia_query, $media_query_clone_ids;

    // Modify the WHERE parameter of the MySQL query
    if ( isset( $media_query_clone_ids['media_ids'] ) && '' != $media_query_clone_ids['media_ids'] ) {
        $where .= " AND $table_name.id IN ( " . $media_query_clone_ids['media_ids'] . ' )';
    }

    return $where;
}

/**
 * Remove `rtmedia-model-where-query` filter once our job is done
 * so that it wont affect the other shortcodes
 */
function my_remove_rtmedia_model_shortcode_query_attributes() {
    remove_filter( 'rtmedia-model-where-query', 'my_rtmedia_model_shortcode_where_query_attributes', 10, 3 );
}
add_action( 'rtmedia_before_media_gallery', 'my_remove_rtmedia_model_shortcode_query_attributes', 10, 3 );

/**
* Sets `media_ids` parameter in rtmedia query
*
* @param type $param
*
* @return array
*/
function my_rtmedia_allowed_attributes_parameter_in_query( $param = array() ) {
    $param[] = 'media_ids';
    return $param;
}
add_filter( 'rtmedia_allowed_query', 'my_rtmedia_allowed_attributes_parameter_in_query', 99 );