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 |1 Answer
Reset to default 1I 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
版权声明:本文标题:wp query - How to do meta_query for attachments? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744753722a2623334.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 whatimage_category
is and how it's being set. – Howdy_McGee ♦ Commented Apr 28, 2017 at 18:41$query->request
after the query is run to see the SQL that was sent to the database. – Milo Commented Apr 29, 2017 at 21:32