Support

Account

Home Forums Add-ons Gallery Field “Reverse Lookup” via Attachment Custom Field Reply To: “Reverse Lookup” via Attachment Custom Field

  • 
    // first you do a query for attachments looking for the exact image_meta_image_id
    $args = array(
        'fields' => 'ids', // return only IDs to use in the next query
        'posts_per_page'    => 1, // there should only be one image 
                                  // with this "image_meta_image_id"
        'post_type'      => 'attachment',
        'meta_query'    => array(
      		array(
      			'key'	 	=> 'image_meta_image_id',
      			'value'	  	=> $image_meta_image_id
      		)
      	)
    );
    $attachment_query = new WP_Query();
    $attachments = $attachment_query->posts();
    
    // now you need to do a query to get all of the posts what use that attachment
    // if it was found
    if (!empty($attachments)) {
      $args = array(
        'post_type' = 'your post type',
        'posts_per_page' => -1,
        'meta_query' => array(
          array(
            'key' => 'gallery field name',
            'value' => '"'.$attachments[0].'"',
            'compare' => 'LIKE'
          )
        )
      ); 
    } // end if attachments found