Skip to content

Instantly share code, notes, and snippets.

@rahul3883
Last active April 13, 2017 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahul3883/958a4da7183b99dbd2d8acf8c148a7a7 to your computer and use it in GitHub Desktop.
Save rahul3883/958a4da7183b99dbd2d8acf8c148a7a7 to your computer and use it in GitHub Desktop.
Sort media by rtMedia custom attributes
<?php
/**
* This code snippet enhances the sort functionality provided by rtMedia Sorting plugin by providing sorting using
* custom attributes that is created by rtMedia Custom Attributes plugin.
* Both rtMedia Custom Attributes and rtMedia Sorting plugins must be enabled for this code snippet to work.
*/
// Enter attribute slug here.
$rtm_attributes_slug = 'camera';
// Enter term slug here.
$rtm_term_slug = array(
'canon',
'nikon',
);
if ( is_plugin_active( 'rtmedia-custom-attributes/index.php' ) && is_plugin_active( 'rtmedia-sorting/index.php' ) ) {
$tmp_rtm_add_attributes_cb = function() use ( $rtm_attributes_slug, $rtm_term_slug ) {
$tmp_rtm_attr_model = new RTMediaAttributesModel();
$attribute_label = $tmp_rtm_attr_model->get_attribute_label( $rtm_attributes_slug );
if ( ! empty( $attribute_label ) ) {
$taxonomy_name = rtmedia_attribute_taxonomy_name( $rtm_attributes_slug );
$terms = get_terms( array(
'taxonomy' => $taxonomy_name,
) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $rtm_term_slug, true ) ) {
$tmp_rtm_change_sort_parameter_cb = function( $rtmedia_sort_labels ) use ( $term ) {
$rtmedia_sort_labels[ $term->slug ] = $term->name;
return $rtmedia_sort_labels;
};
add_filter( 'rtmedia_change_sort_parameters_label', $tmp_rtm_change_sort_parameter_cb, 99 );
$tmp_rtm_media_query_cb = function( $media_query ) use ( $term ) {
if ( ! empty( $media_query['sort_parameters'] ) ) {
$media_query['sort_parameters'] .= ',' . $term->slug;
}
return $media_query;
};
add_filter( 'rtmedia_media_query', $tmp_rtm_media_query_cb, 9 );
$tmp_rtm_query_filter_cb = function( $args ) use ( $term, $taxonomy_name ) {
$data = wp_unslash( $_GET ); // Input var okay.
if ( ! empty( sanitize_text_field( $data['json'] ) ) ) {
if ( ! empty( sanitize_text_field( $data['sort_by'] ) ) ) {
switch ( sanitize_text_field( $data['sort_by'] ) ) {
case $term->slug :
$args['order_by'] = 'media_id';
$tmp_rtm_model_join_query_cb = function( $join, $table_name ) use ( $term, $taxonomy_name ) {
$join .= "JOIN wp_term_relationships
ON wp_term_relationships.object_id = {$table_name}.media_id
JOIN wp_term_taxonomy
ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id AND wp_term_taxonomy.taxonomy = '" . $taxonomy_name . "'
JOIN wp_terms
ON wp_terms.term_id = wp_term_taxonomy.term_id AND wp_terms.slug = '" . $term->slug . "'";
return $join;
};
add_filter( 'rtmedia-model-join-query', $tmp_rtm_model_join_query_cb, 99, 2 );
break;
}
}
if ( ! empty( sanitize_text_field( $data['sort_order'] ) ) ) {
switch ( sanitize_text_field( $data['sort_order'] ) ) {
case 'asc' :
$args['order'] = 'ASC';
break;
default :
$args['order'] = 'DESC';
break;
}
}
} // End if().
return $args;
};
add_filter( 'rtmedia_query_filter', $tmp_rtm_query_filter_cb, 99 );
} // End if().
} // End foreach().
} // End if().
};
add_action( 'init', $tmp_rtm_add_attributes_cb, 99 );
} // End if().
@rahul3883
Copy link
Author

rahul3883 commented Apr 11, 2017

Add the above code to your theme's functions.php file.

Change $rtm_attributes_slug and $rtm_term_slug variable as per your requirement.

Example:
As described here, if you want to have macro and prime term of lens attribute as a sorting parameter,
change top of the code like below:

// Enter attribute slug here.
$rtm_attributes_slug = 'lens';

// Enter term slug here.
$rtm_term_slug = array(
	'macro',
	'prime',
);

If you want to pre-sort the media using custom attributes, you can use the below shortcode:
[rtmedia_gallery attribute_slug="lens" term_slug="macro, prime"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment