0

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.

0
2

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(
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() ) {
    $the_query->the_post();
        // DO SOMETHING
    }
}

/* Restore original Post Data */
wp_reset_postdata();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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