I want to make custom taxonomy (gallery) and terms (name1, name2, name3...) Add images into media tab and then retrieve all images from same term. Look like simple task, but I cant manage it.

I registered new (custom) tax, added some terms to image, but I cannot retrieve any of them. Searched for two days for solution, just can't put all pieces all together.

I want to get list of terms first, and then get list of images from this list.


1 Answer 1


To get all images from the same term you can use WP_Query with the "tax_query" argument:

$args = array(
    'post_status' => 'inherit',
'posts_per_page' => -1,
'post_type' => 'attachment',

$args['tax_query'] = array(
    'taxonomy' => 'YOUR_CUSTOM_TAXONOMY',
    'terms' => array( 'YOUR_CUSTOM_TAXONOMY_TERM' ),
    'field' => 'slug',

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        // DO SOMETHING

/* Restore original Post Data */

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.