Support

Account

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

Solving

Complex Query with Media Attachment and Custom Post Types

  • Hey guys,
    I have done a lot of custom wp_queries, and ACF including complex ajax filters, etc.

    However; I am at a COMPLETE loss with what I am trying to do now.

    I have a custom post type called Artists.
    Every Artist post type has an ACF Gallery Field called ‘artist_gallery’ with images in it.

    What I want to do, is have a custom wp_query that will loop through ALL attachments that are currently added to an ‘artist_gallery’ from ANY artist post.

    I started by going through with a query to pull all artists, and display their images. I am using a meta_query to speed up the query and only grab artists that have the gallery populated.

    The issue with this, is that each artist has A LOT of photos. So with 1 post per page, and pagination enabled, it just shows all images from that one artist where I would rather have it loop through all ‘attachments’ instead of ‘artists’ and then display 30 images per page (optimized).

    But, I am having an issue with how to do the meta query to only grab attacmhments that have already been added to a post type of artist, and the custom field of ‘artist_gallery’.

    I am not sure how to do this as I have ‘post_type’ => ‘artist’, as an argument already.

    Is there some way to use the meta key from artists to filter the attachments without first looping through artists?

    I was thinking of a nested query but that would have the same issues.

    Any insight to help me wrap my head around this would be greatly appreciated.

    I haven’t added my query because I feel it may just confuse things. I am hoping to have some brainstorming help on how to do this, not the actual code. I should be able to figure out the syntax but I am lost on the best way to do this from a theory perspective first.

    Thanks in advance guys!

  • 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();
    
  • Hey, have you managed to solve/finalize this project? Happy nu year 😉
    I would guess it needs to be done two separate query, using some results from the #1 query to feed the #2. I did smt similar before, where i only wanted to show the images that the photographers placed into their galleries alr.

  • Don’t know if the OP every got this worked out, but it really isn’t possible without doing several queries. Even querying the DB directly would require several queries. Basically you need to get a list of post IDs to use in a second query.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Complex Query with Media Attachment and Custom Post Types’ is closed to new replies.