A
A
alskorpius2021-07-15 20:01:28
WordPress
alskorpius, 2021-07-15 20:01:28

How to get a custom taxonomy field on another taxonomy page?

I have 2 taxonomies and one custom data type:
Taxonomy: Doctors
Taxonomy: Service Categories
Custom Type: Services

On the service category page, I select all posts in a loop and get a list of doctors for these posts. Next, I try to display the custom fields of doctors, but no data is transmitted during any manipulations. Checked on a variable, ID of the doctor transfers correctly.

// получаю ID текущей категории
                  $cat_id_doc = get_queried_object()->term_id;
                  
                  $args = array(
                    'posts_per_page' => 5,
                    'tax_query' => array(
                      
                      array(
                        'taxonomy' => 'uslugi', 
                        'field' => 'id',
                        'terms' => array( $cat_id_doc ) //ID текущей категории
                      )
                    )
                  );

                  $query = new WP_Query( $args );

                  // выбираю циклом всепосты которые привязаны к текущей категории
                  if ( $query->have_posts() ) {
                    while ( $query->have_posts() ) {
                      $query->the_post();
                      

                      //выбираю циклом всех врачей, которые привязаны к постам категории
                    $product_terms = wp_get_object_terms($post->ID, 'vrachi');
                      if( $product_terms && ! is_wp_error($product_terms) ){
                        
                        foreach( $product_terms as $term ){
                        //получаю название врача, для проверки на дубли
                        $namedoc = $term->name;
                        
                        //Пробовал так получать ID врача для кастомных полей - НЕ ВЫДАЕТ 
                        $term_doc = get_queried_object()->term_id;
                        
                        //так тоже пробовал получать ID - НЕ ВЫДАЕТ 
                        $term_doc2 = get_queried_object();
                        
                        //а вот так получаю ID врача 
                        $nameid = $term->term_id;
                        
                        // Получаем и записываем значения произвольных полей в переменные 
                        $familiya = get_field('familiya', $nameid);
                        
                        
                        
                        //делаю проверку, если врач уже был то не пропускаю
                        if (in_array($namedoc, $termini_massiv)) {
                          
                        } else { 
                        echo get_term_link($term); // - ссылка на врача - ВЫВОДИТ
                        echo $familiya;  //- кастомное поле врача - НЕ ВЫВОДИТ
                          
                        
                        }
                        //добавляю название текущего в массив, чтобы проверить при следующем цикле
                        $termini_massiv[] = $term->name;
                        
                        }
                        

                      }
                    }
                  } else {
                    // Постов не найдено
                  }
                  // Возвращаем оригинальные данные поста. Сбрасываем $post.
                  wp_reset_postdata();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alskorpius, 2021-07-15
@alskorpius

the solution turned out to be simple to disgrace:
<?php the_field('familiya', $term); ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question