admin管理员组

文章数量:1390822

How do I do proper meta_query for attachments? This doesn't show any results

$args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png',  
    'posts_per_page' => 200, 
    'post_status' => 'inherit',
    'meta_query' => array(
        array(
            'key' => 'image_category', 
            'value' => 102, 
            'compare' => '='
            )
        )
    );

$query = new WP_Query($args);

if ($query->have_posts()) : 
         while ($query->have_posts()) : $query->the_post();
              echo get_post_meta(get_the_ID(), 'image_category', true);
         endwhile; 
   endif; 

If I remove args for meta_query then echoing get_post_meta(get_the_ID(), 'image_category', true); shows that there are plenty of attachments with image_category 102

I have set meta for each attachment by using this code

$args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png',  
    'post_status' => 'all',  
    'posts_per_page' => -1, 
    );

$query = new WP_Query($args);

if ($query->have_posts()) 
{
    while($query->have_posts())
    {
        $query->the_post(); 
        global $post;
        ... // Get post category ID to which image is attached to
        update_post_meta($post->ID, 'image_category', $post_category);
    }

}

Basically, I am trying to have a page which will show all images based on post category. If I have a post Apples which contains images of apples and this post is in category Plants, then the query should list all images of Plants.

Now I am not sure if it would be better to just store this images as a Custom Post Type and to have its category as its own regular category, the downside would be that I would need to have 20 000 more rows in a table just to store them hm...

How do I do proper meta_query for attachments? This doesn't show any results

$args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png',  
    'posts_per_page' => 200, 
    'post_status' => 'inherit',
    'meta_query' => array(
        array(
            'key' => 'image_category', 
            'value' => 102, 
            'compare' => '='
            )
        )
    );

$query = new WP_Query($args);

if ($query->have_posts()) : 
         while ($query->have_posts()) : $query->the_post();
              echo get_post_meta(get_the_ID(), 'image_category', true);
         endwhile; 
   endif; 

If I remove args for meta_query then echoing get_post_meta(get_the_ID(), 'image_category', true); shows that there are plenty of attachments with image_category 102

I have set meta for each attachment by using this code

$args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png',  
    'post_status' => 'all',  
    'posts_per_page' => -1, 
    );

$query = new WP_Query($args);

if ($query->have_posts()) 
{
    while($query->have_posts())
    {
        $query->the_post(); 
        global $post;
        ... // Get post category ID to which image is attached to
        update_post_meta($post->ID, 'image_category', $post_category);
    }

}

Basically, I am trying to have a page which will show all images based on post category. If I have a post Apples which contains images of apples and this post is in category Plants, then the query should list all images of Plants.

Now I am not sure if it would be better to just store this images as a Custom Post Type and to have its category as its own regular category, the downside would be that I would need to have 20 000 more rows in a table just to store them hm...

Share Improve this question edited Apr 29, 2017 at 7:41 Marko asked Apr 28, 2017 at 18:32 MarkoMarko 6078 silver badges21 bronze badges 4
  • How is image_category being set? Is it being set on the actual image post or the post the image is attached to? I.E. Can you edit your question and explain what image_category is and how it's being set. – Howdy_McGee Commented Apr 28, 2017 at 18:41
  • 1 You can use taxonomies with attachments, not sure why you're saving it in post meta. Taxonomy queries are also much more resource efficient than meta queries. – Milo Commented Apr 29, 2017 at 8:22
  • Thanks, I didn't know about that, it seems so simple now.. – Marko Commented Apr 29, 2017 at 20:05
  • 1 @Marko If you want to know why meta queries didn't work, it often helps to print $query->requestafter the query is run to see the SQL that was sent to the database. – Milo Commented Apr 29, 2017 at 21:32
Add a comment  | 

1 Answer 1

Reset to default 1

I went with custom taxonomy instead of custom meta field and it works great (although I am still not sure why meta_query didn't work)

I have saved custom taxonomy term for each attachment

wp_set_object_terms($post->ID, $slug, 'image_category' );

And custom query args are

$args = array(
        'post_type' => 'attachment',
        'posts_per_page' => 44,
        'post_status' => 'inherit',
        'post_parent' => null,
        'tax_query' = array(
            array(
                'taxonomy'     => 'image_category',
                'field'   => 'slug',
                'terms' => $cat
                )
            )
        );

本文标签: wp queryHow to do metaquery for attachments