Skip to content

Instantly share code, notes, and snippets.

@thecodepoetry
Created April 5, 2019 10:59
Show Gist options
  • Select an option

  • Save thecodepoetry/5252f7a830fcefdff8d1531975f273d9 to your computer and use it in GitHub Desktop.

Select an option

Save thecodepoetry/5252f7a830fcefdff8d1531975f273d9 to your computer and use it in GitHub Desktop.
Photos masnory grid - image order by date
add_action( 'init', function() {
class My_Shortcode_Override extends DT_Shortcode_GalleryMasonry {
protected function get_albums_attachments( $args = array() ) {
$post_type = $this->get_att( 'post_type' );
$defaults = array(
'orderby' => 'date',
'order' => 'DESC',
'number' => false,
'category' => array(),
//'select' => 'all'
);
$args = wp_parse_args( $args, $defaults );
if ( 'posts' === $post_type ) {
$page_query = $this->get_posts_by_post_type( 'dt_gallery', $this->get_att( 'posts' ) );
} else {
$category_terms = presscore_sanitize_explode_string( $this->get_att( 'category' ) );
$category_field = ( is_numeric( $category_terms[0] ) ? 'term_id' : 'slug' );
$page_query = $this->get_posts_by_taxonomy( 'dt_gallery', 'dt_gallery_category', $category_terms, $category_field );
}
//$page_query = $this->get_posts_by_terms( $args );
$media_items = array(0);
if ( $page_query->have_posts() ) {
$media_items = array();
foreach ( $page_query->posts as $gallery ) {
$gallery_media = get_post_meta( $gallery->ID, '_dt_album_media_items', true );
if ( is_array( $gallery_media ) ) {
$media_items = array_merge( $media_items, $gallery_media );
}
}
}
$media_items = array_unique( $media_items );
$media_args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'post__in' => $media_items,
'orderby' => 'date',
'order' => 'DESC',
'suppress_filters' => false,
);
$pagination_mode = $this->get_att( 'loading_mode' );
$posts_per_page = $this->get_posts_per_page( $pagination_mode );
if ( $args['number'] ) {
$media_args['posts_per_page'] = $posts_per_page ;
}
if ( 'standard' === $pagination_mode ) {
$media_args['paged'] = dt_get_paged_var();
}
return new WP_Query( $media_args );
}
}
add_shortcode( 'dt_gallery_photos_masonry', array( new My_Shortcode_Override(), 'shortcode' ) );
}, 99 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment