基于自定义字段的术语名称(动态)

I want to get terms based on a custom field. I created some custom fields as terms that you can choose with a dropdown. For example: I want to display related posts to a single posts by choosing the term.

Thank you for your Help.

<?php
$terms = wp_get_post_terms( $post->ID, 'referenzen_kategorie'); 
$terms_ids = [];

foreach ( $terms as $term ) {
    $terms_ids[] = $term->term_id;
}

$args = array(
    'post_type' => 'referenzen',
    'orderby' => 'asc',
    'posts_per_page' => 3,
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'referenzen_kategorie',
            'field'    => 'slug',
            'terms'    => bauberatung
        )
    ),
);                        

$query = new WP_Query($args);
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
?>
    <?php $query->the_post();?>
<?php } } ?> 

The code that I provide works but I want to replace "bauberatung" with a custom field so that it dynamically displays.

Yay, i got it work ;)

Here my solution:

  <?php

                $terms = get_field('your_custom_field'); 

                $args = array(
                    'post_type' => 'your_post_type',
                    'orderby' => 'asc',
                    'posts_per_page' => 3,
                    'tax_query' => array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'your_taxonomy',
                            'field'    => 'slug',
                            'terms'    => $terms
                        )
                    ),
                );                        

                $query = new WP_Query($args);
                if ( $query->have_posts() ) {
                    while ( $query->have_posts() ) {
                ?>


                   <?php $query->the_post();?>

With this your able get posts from a specific taxonomy -> term based on custom field (outside the loop), for Example to display to single Services posts related references.

Hope you will find it helpful Cheers!