eggman eggman - 12 days ago 9
PHP Question

Get images from gallery in taxonomy

There seems to be several answers to similar questions but I have not yet found one working for me.

I have a custom post-type called

entertainement
.
entertainement
has a taxonomy called
ent_categories
.

One of the
ent_categories
is called
Event


Each
Event
has a gallery and I am trying to make a query that will return the latest 10 images that has been added to any of the CPT
entertainment
with the category
Event
.

Im hoping to receive a list of urls in an array.

From what I read here something like this should do the trick:

$arg = array(
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_type' => 'attachment',
);


$arg['tax_query'] = array(
array(
'taxonomy' => 'ent_categories',
'field' => 'name',
'terms' => array( 'Event' ),

),
);

$the_query = new WP_Query( $arg );
var_dump($the_query);


The
var_dump($the_query);
displays a lot of things but no images?
Any tips on this one?
Thank you

EDIT:

I just saw that I can do this:

function pw_show_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() )
return $content;
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post );
$image_list = '<ul>';
// Loop through all galleries found
foreach( $galleries as $gallery ) {
// Loop through each image in each gallery
foreach( $gallery as $image ) {
$image_list .= '<li>' . $image . '</li>';
}
}
$image_list .= '</ul>';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
add_filter( 'the_content', 'pw_show_gallery_image_urls' );


This result in that all the gallery images urls get displayed below the images in the gallery.
Maybe this function could be called from a page instead than from functions.php?

Answer

You were on the right track but, you're querying for posts of type attachment with a term of the ent_categories taxonomy, and there will be none of them as you'll see if you:

var_dump($the_query->posts);

If you dump all $the_query your see lots of things because it's a WP_Query object.

You need to query your entertainement posts: (Be careful because you have a typo in your slug!)

$arg = array(
    'posts_per_page' => -1,
    'post_type'      => 'entertainement',
);


$arg['tax_query'] = array(
    array(
        'taxonomy' => 'ent_categories',
        'field'    => 'name',
        'terms'    => 'Event',

    ),
);

$the_query = new WP_Query( $arg );

Then you can iterate the posts and get the gallery items like this:

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        if (get_post_gallery()) :
            echo get_post_gallery();
            print_r(get_post_gallery_images());
        endif;
    }
    /* Restore original Post Data */
    wp_reset_postdata();
}

get_post_gallery_images() will get you an array of the URL's of gallery images

get_post_gallery() will get you actual HTML to print the gallery.