Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #417353

    Hi folks,

    I had the problem of sorting a masonry gallery by random – inclunding using the ‘load more’-functionality. It took a bit of digging into the source code, but I want to give others the results as a hint. In my special case I want to have all images sorted into a special WP Media Folder as well as the post image used in a portfolio item. You may adapt the solution to Your specific needs:

    add_filter('avia_masonry_entries_query', 'avia_gallery_image_query', 10, 2);
    
    function avia_gallery_image_query($query, $params)
    {
    	// Matches on empty masonry galleries which are not Portfolio Masonry
    	if ($params['ids'] == null && (!isset ($params ['taxonomy']) || $params ['taxonomy'] != 'portfolio_entries'))
        {        
    		// First call - then then 'post__in' is set, but empty
    		if (isset ($query ['post__in']))
    		{
    			// Get al my portfolio categories
    			$terms = array();
    			$allTax = get_terms('portfolio_entries');
    			foreach($allTax as $tax) 
    			{
    				$terms[] = $tax->term_id;
    			}
    
    			// Register the WP Media Folder category - doesn't work without
                register_taxonomy('wpmf-category', 'attachment', array('hierarchical'=>true,'show_in_nav_menus'=>false,'show_ui'=>false));
    
    			// Get all the images that match the root folder 'galerie' as well as all portfolio-items
    			$available = new WP_Query
                (
    				array
    				(
    					'posts_per_page' => -1,
    					'post_status'     => array ( 'publish', 'inherit'),
    					'post_type'     => array ( 'attachment', 'portfolio' ),
    					'post_parent'   => null,        
    					'tax_query'    => array
    							(
    								array
    								(            
    									'taxonomy' => 'portfolio_entries',
    									'field'    => 'id',
    									'terms'    => $terms,
    									'operator' => 'IN'
    								),
    								'relation' => 'OR',
    								array
    								(            
    									'taxonomy' => 'wpmf-category',
    									'field'    => 'slug',
    									'terms'    => array ( 'galerie' ),
    									'operator' => 'IN'
    								),                                
    							),
    						'fields'        => 'ids'
                ));
    
    			// Place it into a session variable
    			$_SESSION['gallery'] = $available->posts;
    
    			// Sort it by random
    			shuffle ($_SESSION['gallery']);
    		}
    
    		// Reset all relevant query parameters and set the IN-Clause to the found items and sort them by the random order as done before
    		$query ['post__in'] = $_SESSION['gallery'];
    		$query ['orderby'] = 'post__in';
    		$query ['order'] = 'ASC';
    		$query ['tax_query'] = null;
    		$query ['post_status'] = 'any';
    		$query ['post_type'] = 'any';
    		$query ['post_mime_type'] = null;
    		$query ['post_parent'] = null;
    
    		// Don't let any sort plugin modify what we want
    		remove_all_filters('posts_orderby');
        }
        else
        {
    		// Reset the session variable
    		$_SESSION['gallery'] = array ();
        }
    
        return $query;
    
    }
    
    #417812

    Hey GeorgLudwig!

    Thank you for using Enfold and for sharing this. :)

    Regards,
    Ismael

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.