Support

Account

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

Solved

“Reverse Lookup” via Attachment Custom Field

  • Hey there.

    I’m trying to do a “reverse lookup” I believe it is called. I have a post type “plants” with a Gallery field “main_info_images”. The images themselves have ACF fields, one of which is “image_meta_image_id” (this is not the post ID of the image).

    I need to find the plant that has a gallery image with a certain “image_meta_image_id” number.

    Some of the images were initially uploaded to a different post type, so not all are “attached” to the plant. From what I understand, the image stays attached to its original “parent” post.

    I think I need to do some sort of nested meta_query? Or perhaps two queries — one that searches through all the images by “image_meta_image_id” and returns the post_id of that image, then use that image’s post_id to find the plant that has that image in its Gallery?

    This is what I have to try to get the image’s post_id.

    Thanks so much for any help!

    
    $args = array(
        'numberposts'    => -1,
        'posts_per_page' => 2,
        'post_type'      => 'attachment',
        'meta_query'    => array(
      		array(
      			'key'	 	=> 'image_meta_image_id',
      			'value'	  	=> '"'.$image_meta_image_id.'"',
      			'compare' 	=> 'LIKE'
      		)
      	)
    );
    
    $the_query = new WP_Query( $args );
    if( $the_query->have_posts() ):
      while( $the_query->have_posts() ) : $the_query->the_post();
        $the_image_id = get_the_ID();
        echo "THE IMAGE ID:  ".$the_image_id;
      endwhile;
    endif;
    
    wp_reset_query();
    
    
  • 
    // 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
    
  • Thanks for the help. This makes sense to me, but I’m getting bool(false) when I var_dump the first query.

    Using this:

    
      $image_meta_image_id = $_GET["image_id"];
      echo $image_meta_image_id;  // doing this to make sure it's getting it from the URL.
      // 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( $args );
    
      $attachments = $attachment_query->posts();
    
      echo "<pre>";
      var_dump( $attachments );
      echo "</pre>";
    

    I’ve confirmed that the image I’m trying to get has the correct value for its image_meta_image_id field (It’s 17574).

    Any ideas?

    Thanks a bunch for the help!

  • Oh, and in case it matters, my Media Library has 102,000 items in it and wp_postmeta database table has about 16 million rows.

  • Not sure what is wrong with the first query. If it is returning false then it’s not finding any posts (attachments) that match. Number of posts or meta rows should not matter.

    Try dumping $attachment_query

  • Here’s var_dump( $attachment_query );

    
    object(WP_Query)#3978 (52) {
      ["query"]=>
      array(4) {
        ["fields"]=>
        string(3) "ids"
        ["posts_per_page"]=>
        int(1)
        ["post_type"]=>
        string(10) "attachment"
        ["meta_query"]=>
        array(1) {
          [0]=>
          array(2) {
            ["key"]=>
            string(19) "image_meta_image_id"
            ["value"]=>
            string(5) "17574"
          }
        }
      }
      ["query_vars"]=>
      array(66) {
        ["fields"]=>
        string(3) "ids"
        ["posts_per_page"]=>
        int(1)
        ["post_type"]=>
        string(10) "attachment"
        ["meta_query"]=>
        array(1) {
          [0]=>
          array(2) {
            ["key"]=>
            string(19) "image_meta_image_id"
            ["value"]=>
            string(5) "17574"
          }
        }
        ["error"]=>
        string(0) ""
        ["m"]=>
        string(0) ""
        ["p"]=>
        int(0)
        ["post_parent"]=>
        string(0) ""
        ["subpost"]=>
        string(0) ""
        ["subpost_id"]=>
        string(0) ""
        ["attachment"]=>
        string(0) ""
        ["attachment_id"]=>
        int(0)
        ["name"]=>
        string(0) ""
        ["pagename"]=>
        string(0) ""
        ["page_id"]=>
        int(0)
        ["second"]=>
        string(0) ""
        ["minute"]=>
        string(0) ""
        ["hour"]=>
        string(0) ""
        ["day"]=>
        int(0)
        ["monthnum"]=>
        int(0)
        ["year"]=>
        int(0)
        ["w"]=>
        int(0)
        ["category_name"]=>
        string(0) ""
        ["tag"]=>
        string(0) ""
        ["cat"]=>
        string(0) ""
        ["tag_id"]=>
        string(0) ""
        ["author"]=>
        string(0) ""
        ["author_name"]=>
        string(0) ""
        ["feed"]=>
        string(0) ""
        ["tb"]=>
        string(0) ""
        ["paged"]=>
        int(0)
        ["meta_key"]=>
        string(0) ""
        ["meta_value"]=>
        string(0) ""
        ["preview"]=>
        string(0) ""
        ["s"]=>
        string(0) ""
        ["sentence"]=>
        string(0) ""
        ["title"]=>
        string(0) ""
        ["menu_order"]=>
        string(0) ""
        ["embed"]=>
        string(0) ""
        ["category__in"]=>
        array(0) {
        }
        ["category__not_in"]=>
        array(0) {
        }
        ["category__and"]=>
        array(0) {
        }
        ["post__in"]=>
        array(0) {
        }
        ["post__not_in"]=>
        array(0) {
        }
        ["post_name__in"]=>
        array(0) {
        }
        ["tag__in"]=>
        array(0) {
        }
        ["tag__not_in"]=>
        array(0) {
        }
        ["tag__and"]=>
        array(0) {
        }
        ["tag_slug__in"]=>
        array(0) {
        }
        ["tag_slug__and"]=>
        array(0) {
        }
        ["post_parent__in"]=>
        array(0) {
        }
        ["post_parent__not_in"]=>
        array(0) {
        }
        ["author__in"]=>
        array(0) {
        }
        ["author__not_in"]=>
        array(0) {
        }
        ["search_columns"]=>
        array(0) {
        }
        ["ignore_sticky_posts"]=>
        bool(false)
        ["suppress_filters"]=>
        bool(false)
        ["cache_results"]=>
        bool(true)
        ["update_post_term_cache"]=>
        bool(true)
        ["update_menu_item_cache"]=>
        bool(false)
        ["lazy_load_term_meta"]=>
        bool(true)
        ["update_post_meta_cache"]=>
        bool(true)
        ["nopaging"]=>
        bool(false)
        ["comments_per_page"]=>
        string(2) "50"
        ["no_found_rows"]=>
        bool(false)
        ["order"]=>
        string(4) "DESC"
      }
      ["tax_query"]=>
      object(WP_Tax_Query)#3417 (6) {
        ["queries"]=>
        array(0) {
        }
        ["relation"]=>
        string(3) "AND"
        ["table_aliases":protected]=>
        array(0) {
        }
        ["queried_terms"]=>
        array(0) {
        }
        ["primary_table"]=>
        string(8) "wp_posts"
        ["primary_id_column"]=>
        string(2) "ID"
      }
      ["meta_query"]=>
      object(WP_Meta_Query)#3983 (9) {
        ["queries"]=>
        array(2) {
          [0]=>
          array(2) {
            ["key"]=>
            string(19) "image_meta_image_id"
            ["value"]=>
            string(5) "17574"
          }
          ["relation"]=>
          string(2) "OR"
        }
        ["relation"]=>
        string(3) "AND"
        ["meta_table"]=>
        string(11) "wp_postmeta"
        ["meta_id_column"]=>
        string(7) "post_id"
        ["primary_table"]=>
        string(8) "wp_posts"
        ["primary_id_column"]=>
        string(2) "ID"
        ["table_aliases":protected]=>
        array(1) {
          [0]=>
          string(11) "wp_postmeta"
        }
        ["clauses":protected]=>
        array(1) {
          ["wp_postmeta"]=>
          array(6) {
            ["key"]=>
            string(19) "image_meta_image_id"
            ["value"]=>
            string(5) "17574"
            ["compare"]=>
            string(1) "="
            ["compare_key"]=>
            string(1) "="
            ["alias"]=>
            string(11) "wp_postmeta"
            ["cast"]=>
            string(4) "CHAR"
          }
        }
        ["has_or_relation":protected]=>
        bool(false)
      }
      ["date_query"]=>
      bool(false)
      ["request"]=>
      string(472) "
    			SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID
    			FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
    			WHERE 1=1  AND ( 
      ( wp_postmeta.meta_key = 'image_meta_image_id' AND wp_postmeta.meta_value = '17574' )
    ) AND ((wp_posts.post_type = 'attachment' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' 
    OR wp_posts.post_status = 'private')))
    			GROUP BY wp_posts.ID
    			ORDER BY wp_posts.post_date DESC
    			LIMIT 0, 1
    		"
      ["posts"]=>
      array(0) {
      }
      ["post_count"]=>
      int(0)
      ["current_post"]=>
      int(-1)
      ["before_loop"]=>
      bool(true)
      ["in_the_loop"]=>
      bool(false)
      ["comment_count"]=>
      int(0)
      ["current_comment"]=>
      int(-1)
      ["found_posts"]=>
      int(0)
      ["max_num_pages"]=>
      int(0)
      ["max_num_comment_pages"]=>
      int(0)
      ["is_single"]=>
      bool(false)
      ["is_preview"]=>
      bool(false)
      ["is_page"]=>
      bool(false)
      ["is_archive"]=>
      bool(false)
      ["is_date"]=>
      bool(false)
      ["is_year"]=>
      bool(false)
      ["is_month"]=>
      bool(false)
      ["is_day"]=>
      bool(false)
      ["is_time"]=>
      bool(false)
      ["is_author"]=>
      bool(false)
      ["is_category"]=>
      bool(false)
      ["is_tag"]=>
      bool(false)
      ["is_tax"]=>
      bool(false)
      ["is_search"]=>
      bool(false)
      ["is_feed"]=>
      bool(false)
      ["is_comment_feed"]=>
      bool(false)
      ["is_trackback"]=>
      bool(false)
      ["is_home"]=>
      bool(true)
      ["is_privacy_policy"]=>
      bool(false)
      ["is_404"]=>
      bool(false)
      ["is_embed"]=>
      bool(false)
      ["is_paged"]=>
      bool(false)
      ["is_admin"]=>
      bool(false)
      ["is_attachment"]=>
      bool(false)
      ["is_singular"]=>
      bool(false)
      ["is_robots"]=>
      bool(false)
      ["is_favicon"]=>
      bool(false)
      ["is_posts_page"]=>
      bool(false)
      ["is_post_type_archive"]=>
      bool(false)
      ["query_vars_hash":"WP_Query":private]=>
      string(32) "79faba4333b88670ed32918a2aeeae58"
      ["query_vars_changed":"WP_Query":private]=>
      bool(false)
      ["thumbnails_cached"]=>
      bool(false)
      ["allow_query_attachment_by_filename":protected]=>
      bool(false)
      ["stopwords":"WP_Query":private]=>
      NULL
      ["compat_fields":"WP_Query":private]=>
      array(2) {
        [0]=>
        string(15) "query_vars_hash"
        [1]=>
        string(18) "query_vars_changed"
      }
      ["compat_methods":"WP_Query":private]=>
      array(2) {
        [0]=>
        string(16) "init_query_flags"
        [1]=>
        string(15) "parse_tax_query"
      }
    }
    
  • Here’s what I think is the complete rundown:

    I’m using this URL:

    /testing/?image_id=8324

    There’s nothing on that page except for:

    [test_shortcode]

    And just to confirm I’m using the correct field name, here’s the source code of the field in the backend when editing an image:

    
    <tr class="acf-field acf-field-number acf-field-64e143e0efab0" data-name="image_meta_image_id" data-type="number" data-key="field_64e143e0efab0">
    <td class="acf-label">
    <label for="acf-field_64e143e0efab0">Image ID</label></td>
    <td class="acf-input">
    <div class="acf-input-wrap"><input type="number" id="acf-field_64e143e0efab0" name="acf[field_64e143e0efab0]" value="8324" step="any"></div></td>
    </tr>
    

    And here’s the complete shortcode:

    
    function get_test_shortcode() {
      $image_meta_image_id = $_GET["image_id"];
      // echo $image_meta_image_id;
      // *********************** FROM ACF FORUM 09-01-2023 ***********************
      // 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( $args );
      $attachments = $attachment_query->posts();
    
    echo "<pre>";
      var_dump( $attachments );
    echo "</pre>";
    
    }
    add_shortcode( 'test_shortcode', 'get_test_shortcode' );
    

    The only thing showing on the page is the page title TESTING and bool(false).

    Do you see anything amiss?

    Thanks again!!

  • I tailed the error logs and see this when I hit the page:

    [Sun Sep 03 13:10:19.143344 2023] [proxy_fcgi:error] [pid 315244:tid 315431] [client 172.69.205.140:45548] AH01071: Got error ‘PHP message: PHP Warning: Undefined array key “plant_id” in <PATH HERE — REMOVED>/shortcodes/gallery-image.php on line 6’

    Adding it here in case it may be relevant.

  • I don’t see anything wrong with this except that a shortcode should return the value, not output it

    
    // somewhere at the top of your shourtcode function
    ob_start();
    
    return ob_get_clean();
    // at the bottom or your shortcode function
    

    The only reason that it is returning no posts is that it has found no posts. This means that:

    1. the meta key you are searching on is wrong
    2. the attachment post does not have a value in this meta key
    3. there are no attachement posts with this meta value
  • Hmm… there must be something else going on here. There is an attachment post with the correct key and value.

    When I run this:

    SELECT * FROM wp_postmeta WHERE meta_key = image_meta_image_id AND meta_value = 16233

    I get this:

    <table name=”wp_postmeta”>
    <column name=”meta_id”>15708754</column>
    <column name=”post_id”>303198</column>
    <column name=”meta_key”>image_meta_image_id</column>
    <column name=”meta_value”>16233</column>
    </table>

    Then running this to get the post info:

    SELECT * FROM wp_posts WHERE ID = 303198

    Gets me this:

    <table name=”wp_posts”>
    <column name=”ID”>303198</column>
    <column name=”post_author”>1</column>
    <column name=”post_date”>2023-08-16 01:25:29</column>
    <column name=”post_date_gmt”>2023-08-16 06:25:29</column>
    <column name=”post_content”/>
    <column name=”post_title”>PCD3960_IMG0039.jpg</column>
    <column name=”post_excerpt”/>
    <column name=”post_status”>publish</column>
    <column name=”comment_status”>open</column>
    <column name=”ping_status”>closed</column>
    <column name=”post_password”/>
    <column name=”post_name”>pcd3960_img0039-jpg</column>
    <column name=”to_ping”/>
    <column name=”pinged”/>
    <column name=”post_modified”>2023-08-30 20:14:21</column>
    <column name=”post_modified_gmt”>2023-08-31 01:14:21</column>
    <column name=”post_content_filtered”/>
    <column name=”post_parent”>0</column>
    <column name=”guid”>
    PATH TO IMAGE HERE – REMOVED FOR FORUM POST
    </column>
    <column name=”menu_order”>0</column>
    <column name=”post_type”>attachment</column>
    <column name=”post_mime_type”>image/jpeg</column>
    <column name=”comment_count”>0</column>
    </table>

    Any ideas? Thanks so so much for the help. It’s greatly appreciated!

  • found your issue. You need to add

    
    'post_status' => 'inherit'
    

    to your query. The default status of a WP_Query() is “publish” but the default status for an attachment is “inherit”

  • You can also use “any”

  • Man, still a no-go. Here’s what I have now:

      $args = array(
          'fields' => 'ids',
          'posts_per_page'    => 1, 
          'post_type'      => 'post',
          'post_status' => 'any',
          'meta_query'    => array(
        		array(
        			'key'	 	=> 'image_meta_image_id',
        			'value'	  	=> $image_meta_image_id
        		)
        	)
      );
      $attachment_query = new WP_Query( $args );
      $attachments = $attachment_query->posts();
    

    var_dump( $attachments ); gets me bool(false), but I see it in PMA (attached).

    PMA Screen Shot

    var_dump( $attachment_query ) is below.

    Appreciate any other ideas you might have. Thanks so much!

    
    object(WP_Query)#3567 (52) {
      ["query"]=>
      array(5) {
        ["fields"]=>
        string(3) "ids"
        ["posts_per_page"]=>
        int(1)
        ["post_type"]=>
        string(4) "post"
        ["post_status"]=>
        string(3) "any"
        ["meta_query"]=>
        array(1) {
          [0]=>
          array(2) {
            ["key"]=>
            string(19) "image_meta_image_id"
            ["value"]=>
            string(5) "16233"
          }
        }
      }
      ["query_vars"]=>
      array(67) {
        ["fields"]=>
        string(3) "ids"
        ["posts_per_page"]=>
        int(1)
        ["post_type"]=>
        string(4) "post"
        ["post_status"]=>
        string(3) "any"
        ["meta_query"]=>
        array(1) {
          [0]=>
          array(2) {
            ["key"]=>
            string(19) "image_meta_image_id"
            ["value"]=>
            string(5) "16233"
          }
        }
        ["error"]=>
        string(0) ""
        ["m"]=>
        string(0) ""
        ["p"]=>
        int(0)
        ["post_parent"]=>
        string(0) ""
        ["subpost"]=>
        string(0) ""
        ["subpost_id"]=>
        string(0) ""
        ["attachment"]=>
        string(0) ""
        ["attachment_id"]=>
        int(0)
        ["name"]=>
        string(0) ""
        ["pagename"]=>
        string(0) ""
        ["page_id"]=>
        int(0)
        ["second"]=>
        string(0) ""
        ["minute"]=>
        string(0) ""
        ["hour"]=>
        string(0) ""
        ["day"]=>
        int(0)
        ["monthnum"]=>
        int(0)
        ["year"]=>
        int(0)
        ["w"]=>
        int(0)
        ["category_name"]=>
        string(0) ""
        ["tag"]=>
        string(0) ""
        ["cat"]=>
        string(0) ""
        ["tag_id"]=>
        string(0) ""
        ["author"]=>
        string(0) ""
        ["author_name"]=>
        string(0) ""
        ["feed"]=>
        string(0) ""
        ["tb"]=>
        string(0) ""
        ["paged"]=>
        int(0)
        ["meta_key"]=>
        string(0) ""
        ["meta_value"]=>
        string(0) ""
        ["preview"]=>
        string(0) ""
        ["s"]=>
        string(0) ""
        ["sentence"]=>
        string(0) ""
        ["title"]=>
        string(0) ""
        ["menu_order"]=>
        string(0) ""
        ["embed"]=>
        string(0) ""
        ["category__in"]=>
        array(0) {
        }
        ["category__not_in"]=>
        array(0) {
        }
        ["category__and"]=>
        array(0) {
        }
        ["post__in"]=>
        array(0) {
        }
        ["post__not_in"]=>
        array(0) {
        }
        ["post_name__in"]=>
        array(0) {
        }
        ["tag__in"]=>
        array(0) {
        }
        ["tag__not_in"]=>
        array(0) {
        }
        ["tag__and"]=>
        array(0) {
        }
        ["tag_slug__in"]=>
        array(0) {
        }
        ["tag_slug__and"]=>
        array(0) {
        }
        ["post_parent__in"]=>
        array(0) {
        }
        ["post_parent__not_in"]=>
        array(0) {
        }
        ["author__in"]=>
        array(0) {
        }
        ["author__not_in"]=>
        array(0) {
        }
        ["search_columns"]=>
        array(0) {
        }
        ["ignore_sticky_posts"]=>
        bool(false)
        ["suppress_filters"]=>
        bool(false)
        ["cache_results"]=>
        bool(true)
        ["update_post_term_cache"]=>
        bool(true)
        ["update_menu_item_cache"]=>
        bool(false)
        ["lazy_load_term_meta"]=>
        bool(true)
        ["update_post_meta_cache"]=>
        bool(true)
        ["nopaging"]=>
        bool(false)
        ["comments_per_page"]=>
        string(2) "50"
        ["no_found_rows"]=>
        bool(false)
        ["order"]=>
        string(4) "DESC"
      }
      ["tax_query"]=>
      object(WP_Tax_Query)#3974 (6) {
        ["queries"]=>
        array(0) {
        }
        ["relation"]=>
        string(3) "AND"
        ["table_aliases":protected]=>
        array(0) {
        }
        ["queried_terms"]=>
        array(0) {
        }
        ["primary_table"]=>
        string(8) "wp_posts"
        ["primary_id_column"]=>
        string(2) "ID"
      }
      ["meta_query"]=>
      object(WP_Meta_Query)#3962 (9) {
        ["queries"]=>
        array(2) {
          [0]=>
          array(2) {
            ["key"]=>
            string(19) "image_meta_image_id"
            ["value"]=>
            string(5) "16233"
          }
          ["relation"]=>
          string(2) "OR"
        }
        ["relation"]=>
        string(3) "AND"
        ["meta_table"]=>
        string(11) "wp_postmeta"
        ["meta_id_column"]=>
        string(7) "post_id"
        ["primary_table"]=>
        string(8) "wp_posts"
        ["primary_id_column"]=>
        string(2) "ID"
        ["table_aliases":protected]=>
        array(1) {
          [0]=>
          string(11) "wp_postmeta"
        }
        ["clauses":protected]=>
        array(1) {
          ["wp_postmeta"]=>
          array(6) {
            ["key"]=>
            string(19) "image_meta_image_id"
            ["value"]=>
            string(5) "16233"
            ["compare"]=>
            string(1) "="
            ["compare_key"]=>
            string(1) "="
            ["alias"]=>
            string(11) "wp_postmeta"
            ["cast"]=>
            string(4) "CHAR"
          }
        }
        ["has_or_relation":protected]=>
        bool(false)
      }
      ["date_query"]=>
      bool(false)
      ["request"]=>
      string(426) "
    			SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID
    			FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
    			WHERE 1=1  AND ( 
      ( wp_postmeta.meta_key = 'image_meta_image_id' AND wp_postmeta.meta_value = '16233' )
    ) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status <> 'trash' AND wp_posts.post_status <> 'auto-draft'))
    			GROUP BY wp_posts.ID
    			ORDER BY wp_posts.post_date DESC
    			LIMIT 0, 1
    		"
      ["posts"]=>
      array(0) {
      }
      ["post_count"]=>
      int(0)
      ["current_post"]=>
      int(-1)
      ["before_loop"]=>
      bool(true)
      ["in_the_loop"]=>
      bool(false)
      ["comment_count"]=>
      int(0)
      ["current_comment"]=>
      int(-1)
      ["found_posts"]=>
      int(0)
      ["max_num_pages"]=>
      int(0)
      ["max_num_comment_pages"]=>
      int(0)
      ["is_single"]=>
      bool(false)
      ["is_preview"]=>
      bool(false)
      ["is_page"]=>
      bool(false)
      ["is_archive"]=>
      bool(false)
      ["is_date"]=>
      bool(false)
      ["is_year"]=>
      bool(false)
      ["is_month"]=>
      bool(false)
      ["is_day"]=>
      bool(false)
      ["is_time"]=>
      bool(false)
      ["is_author"]=>
      bool(false)
      ["is_category"]=>
      bool(false)
      ["is_tag"]=>
      bool(false)
      ["is_tax"]=>
      bool(false)
      ["is_search"]=>
      bool(false)
      ["is_feed"]=>
      bool(false)
      ["is_comment_feed"]=>
      bool(false)
      ["is_trackback"]=>
      bool(false)
      ["is_home"]=>
      bool(true)
      ["is_privacy_policy"]=>
      bool(false)
      ["is_404"]=>
      bool(false)
      ["is_embed"]=>
      bool(false)
      ["is_paged"]=>
      bool(false)
      ["is_admin"]=>
      bool(false)
      ["is_attachment"]=>
      bool(false)
      ["is_singular"]=>
      bool(false)
      ["is_robots"]=>
      bool(false)
      ["is_favicon"]=>
      bool(false)
      ["is_posts_page"]=>
      bool(false)
      ["is_post_type_archive"]=>
      bool(false)
      ["query_vars_hash":"WP_Query":private]=>
      string(32) "2ab9be15f5f96057950ea53522e2d60a"
      ["query_vars_changed":"WP_Query":private]=>
      bool(false)
      ["thumbnails_cached"]=>
      bool(false)
      ["allow_query_attachment_by_filename":protected]=>
      bool(false)
      ["stopwords":"WP_Query":private]=>
      NULL
      ["compat_fields":"WP_Query":private]=>
      array(2) {
        [0]=>
        string(15) "query_vars_hash"
        [1]=>
        string(18) "query_vars_changed"
      }
      ["compat_methods":"WP_Query":private]=>
      array(2) {
        [0]=>
        string(16) "init_query_flags"
        [1]=>
        string(15) "parse_tax_query"
      }
    }
    
  • 
    
          'post_type'      => 'post',
    

    should be

    
    
          'post_type'      => 'attachment',
    
  • Ugh…sorry. Had that and still no go:

    
      $args = array(
          'fields' => 'ids',
          'posts_per_page'    => 1,
          'post_type'      => 'attachment',
          'post_status' => 'any',
          'meta_query'    => array(
        		array(
        			'key'	 	=> 'image_meta_image_id',
        			'value'	  	=> $image_meta_image_id
        		)
        	)
      );
      $attachment_query = new WP_Query( $args );
      $attachments = $attachment_query->posts();
    
    
  • I don’t see any reason it should not be working.

  • Ok, ugh! Thank you so much for your help here. I’ll update when I get it figured out in case that will help others.

  • Ended up cheating by using this:

    
    $image_meta_image_id = $_GET["image_id"];
    global $wpdb;
    $attachment_query = "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( ( wp_postmeta.meta_key = 'image_meta_image_id' AND wp_postmeta.meta_value = '" . $image_meta_image_id . "' ) ) AND wp_posts.post_type = 'attachment' AND ((wp_posts.post_status <> 'trash' AND wp_posts.post_status <> 'auto-draft')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 1";
    $attachments = $wpdb->get_results( $attachment_query );
    

    Still unsure why the WP query doesn’t work, but this is good enough. Thanks for the help.

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

You must be logged in to reply to this topic.