The ACF “Taxonomy” field lets you select terms from a taxonomy, not taxonomies themselves. So if you want your selections in that field to be used in your code, you need to:
- Remove the
get_object_taxonomies()
line and the subsequentforeach
. You’re looping through terms, not Taxonomies, - Make sure the Taxonomy field is set to return Term ID. As the primary key, this is likely faster than querying posts by term slugs.
- Replace
get_terms()
with theget_field()
for your custom field. This is what you’ll be looping over. - Change your
tax_query
to useterm_id
as the field, to match the output of the field as set in #2.
That will leave you with something like this:
$post_type="post";
$taxonomy = 'taxonomy_name';
$terms = get_field( 'taxonomy_field_name' );
foreach( $terms as $term ) :
$args = array(
'post_type' => $post_type,
'posts_per_page' => 5,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term,
),
),
);
$posts = new WP_Query( $args );
// Output posts, etc.
endforeach;