I had a scenario very similar to yours. I trust these notes can help.
1 – When querying a custom taxonomy, wp_query requires a more information.
The codex reference is here.
The information is in this form.
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob',
),
),
In your case, I believe that the taxonomy is “artwork”. I found that using the field value of ‘slug’ is good; I leave it to you to know what your term is.
2 – In your revised function, you dropped the “posts_per_page” parameter. That’s your choice but be aware that if you don’t declare it then WP-Query will return a maximum of 10 records. If you were expecting more images then this could be the answer.
3 – In your function “display_images_from_media_library“, the second line is:
“$url = wp_get_attachment_url( $image->ID );”.
In this case, $image is an undefined variable. Since $image is a variable from your function “get_images_from_media_library“, I’ll hazard a guess that at some stage you had a single function for getting the image information and then displaying it; but when you split the single function into two, you probably didn’t notice this line.
4 – Your code to create the filters starts with the top level (the parent level) – that fine, but I believe that the filters are actually the children not the parents. In essence, there’s another foreach required (details below).
5 – In addition to the above, you have made your initial focus the “attachment URL”. But I suggest it might be more productive to concentrate on getting the post/attachment ID from the WP_Query, and use this as a means to get all the other fields that you need to display. In fact, if you create an array based on $posts, you’ll automatically get the caption and the title as a by-product.
I found the following to work. Obviously you need to substitute the relevant Taxonomy and term in each of the functions.
function gallery_get_images_from_media_library(){
$g_myargs = array(
'post_type' => 'attachment',
'post_mime_type' =>'image/jpeg',
'post_status' => 'inherit',
'posts_per_page' => 25,
'tax_query' => array(
array(
'taxonomy' => 'media_category',
'field' => 'slug',
'terms' => 'edition-covers',
),
),
);
$g_myquery_images = new WP_Query( $g_myargs );
$g_posts = $g_myquery_images->posts;
$html="<section class="isotope-wrap">
<div class="isotope gallery" data-pswp-uid="1">
<div class="isotope-sizer"></div>";
foreach ($g_posts as $g_img) {
$url = wp_get_attachment_url($g_img->ID);
$src = wp_get_attachment_image_src($g_img->ID,'full');
$image_title = $g_img->post_title;
$caption = $g_img->post_excerpt;
$html .= '<div class="isotope-item"><figure>
<a href="'.$url.'">
<img src="'.$src[0] .'"></a>
<figcaption class="wp-caption-text">
<h2>' . $image_title . '</h2>
<p>' . $caption . '</p>
</figcaption></figure></div>';
}
$html .= '</div>';
return $html;
}
To display the images I just added the following code in my template page.
$imgs = gallery_get_images_from_media_library();
echo $imgs;
function gallery_make_filters(){
// starting row of the filter = show all
$gallery_filter_content="<button class="button is-checked" data-filter="*">show all</button>". PHP_EOL ;
// get the cover terms for parent terms with a count>0
$gallery_parentterms = get_terms( array(
'taxonomy' => 'reviewcovers',
'parent' => 0,
'hide_empty' => false,
));
// if there are parent covers with children
if (is_array($gallery_parentterms)) {
// break down for each parent
foreach($gallery_parentterms as $gallery_obj) {
// get the children covers
$gallery_childterms = get_terms( array(
'taxonomy' => 'reviewcovers',
'child_of' => $gallery_obj->term_id,
'hide_empty' => true,
));
// test for content of childterms
if (is_array($gallery_childterms)) {
// create a filter for each child.
foreach($gallery_childterms as $gallery_object) {
// build the filters for each non-zero child term
// filter is progressively incremented.
$gallery_filter_content .= '<button class="button" data-filter="'.".".$gallery_object->slug.'">'.$gallery_object->description.'</button>'. PHP_EOL;
} // end of foreach-children array
} // end of if children array
} //end of foreach parent array
}// end of if - parent array
else
{
echo 'no results';
}
return $gallery_filter_content;
}
To display the filters, insert the following code in the page template.
$filters = gallery_make_filters();
echo $filters;
Lastly, and not wanting to be accused of ‘teaching you how to suck eggs’, I find that every so often one or more lines of my code don’t work as expected (probably because I am not such a great coder). In any event, when this does happen, I find it useful to display the contents of an array or a variable so that I can work out just where the code is coming undone.
These few lines are my mainstays. I just ‘unremark’ the lines, update the array/variable name, save, update the site.
1 – for an array
//echo "<pre>";print_r($gallery_childterms);echo "</pre>"; //DEBUG - a readable form of vardump
//echo "<p>end of print_r gallery_childterms</p>"; //DEBUG-this is so I know whether and what has printed.
2 – for a variable
//echo "the src is ".$src."</br>"; // DEBUG - the line break is so that consecutive lines will make sense.