Reputation: 6189
I have the following code and im trying to get the images by category, I have a category name "home-hero" and when I add it into the following code it still displays all the images in the media.
How can i get it to only display all the images under home-hero?
<?php
function get_images_from_media_library() {
$args = array(
'taxonomy' => 'home-hero',
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'order' => 'DESC'
);
$query_images = new WP_Query( $args );
return $query_images;
}
function display_images_from_media_library() {
$imgs = get_images_from_media_library();
$html = '<div id="media-gallery">';
global $post;
if ($imgs->have_posts()) {
while($imgs->have_posts()) {
$imgs->the_post();
$html .= '<img src="' . $post->guid . '" alt="" />';
$html .= get_the_date('F j, Y');
}
}
$html .= '</div>';
return $html;
}
echo display_images_from_media_library();
?>
Thanks for taking the time to ready.
Upvotes: 0
Views: 3391
Reputation: 9941
As I said i a comment, I have a working incomplete solution that I need to finish for a project.
The problem is, attachments don't have categories, just their parents have, so it is impossible with a simple query to get attachments from a specific category
You will need to run two queries here, the first query will get all the parent posts from a given category, the second query will use the parent post ID's to get the attachments from all the parent posts given
In both queries, you just need to get the post ID field, this will dramatically speed up your query, and you will use the returned attachment ID's ($attachments->posts
) with wp_get_attachment_image()
This code was meant to go into a function and $post_type
, $taxonomy
and $term
is supposed to be retrieved dynamically. You can hardcode values to these three variables or also make it dynamic in your function
You don't need to use a tax_query
, you can simply use the category parameters if you use the build-in taxonomy category
. I just used a tax_query
so that the function-to-be can be used for any taxonomy. If you leave the tax_query
for build-in categories, remember, the taxonomy is category
and the term will be the category ID (I've set field to term_id
)
This code requires at least PHP 5.4. If you have a version older than 5.4, simply change the array wrapper []
to array()
Here is the code:
$args1 = [
'posts_per_page' => -1,
'fields' => 'ids',
'post_type' => $post_type,
'tax_query' => [
[
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term,
'include_children' => false
],
],
];
$post_parents = new WP_Query($args1);
if( $post_parents->posts ) {
$args = [
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'fields' => 'ids',
'posts__in' => $post_parents->posts,
];
$attachments = new WP_Query($args);
}
?><pre><?php var_dump($attachments->posts); ?></pre><?php
Hope this helps you in your project
Upvotes: 1
Reputation: 1615
You can something like this :
<?php
$images = get_posts( array('post_type' => 'attachment', 'category__in' => array(cat_ids)) );
if ( !empty($images) ) {
foreach ( $images as $image ) {
echo wp_get_attachment_image($image->ID).'<br />';
echo $image->post_title .'<br />';
the_attachment_link( $image->ID, true );
}
}
?>
Hope this helps...
Upvotes: 0