Support

Account

Home Forums Add-ons Gallery Field Complex Query with Media Attachment and Custom Post Types Reply To: Complex Query with Media Attachment and Custom Post Types

  • This is what I have figured out so far…. but I can’t seem to get the seed to work with the session and keeping random working with pagination.

    Not sure if this is a Genesis issue at this point, my code, or WordPress.

    global $post;
      
    $query = new WP_Query( 
      array( 
        'post_type' => 'artist', 
        'posts_per_page' => -1,
        'fields' => 'ids',
        'meta_query' => array(
        	array(
            	'key' => 'artist_gallery'
                )
    		  )
            ) 
          );
            $seed = $_SESSION['seed'];
            if (empty($seed)) {
              $seed = rand();
              $_SESSION['seed'] = $seed;
            }
            $args = array( 
                'post_type' => 'attachment', 
                'post_status' => 'inherit',
                'orderby' => 'RAND(' . $seed . ')',
                'post_mime_type' => 'image', 
                'posts_per_page' => 30, 
                'post_parent__in' => $query->posts, 
                'order' => 'DESC' 
                );
      
     	global $wp_query;
        $wp_query = new WP_Query( $args ); 
    
        if ( have_posts() ) : 
            while ( have_posts() ) : the_post(); 
    			$image_attachment = get_attachment_link( get_the_ID() );
    			$img_src = wp_get_attachment_image_src( get_the_ID(), 'medium');
    			$alt = get_the_title();
    echo '	
    			<div class="col-md-4"> 
    				<a href='. $image_attachment .'>
    					<img src="'. $img_src[0] .'" alt="'. $alt .'" />
    				</a>
    				<p>'. $image["caption"] .'</p>
    			</div>
    ';
    	endwhile;
    	do_action( 'genesis_after_endwhile' );
    endif;				
    
    // Restore original Post Data
    	wp_reset_postdata();