Как получить сообщение, используя термины таксономии в WordPress - PullRequest
1 голос
/ 06 февраля 2020

У меня есть тип записи под названием "обоснование", и мое название таксономии "company_list". В таксономии есть список компаний. У каждой компании есть много обоснований.

Я хочу получить последнее обоснование для каждой компании. Как я могу это сделать ? Я пытаюсь ниже код, но он показывает весь список компаний, но данные дублируются

<?php
//$taxonomy = 'our_work_thematic';
$myquery = array (
    'post_type' => 'rationale',
    'paged'=>$paged,
    'posts_per_page'   => -1,
);
$loop = new WP_Query($myquery); 
if( $loop->have_posts() ):
    while( $loop->have_posts() ): 
            $loop->the_post(); global $post; ?>
        <?php $terms = get_the_terms( $post->ID, 'company_list' ); 
        foreach($terms as $term) {
            $termlinks = get_term_link($term);
            echo '<p class="post-content--cat">';
                echo '<a href="' . $termlinks . '">' . $term->name . '</a>';  
            echo '</p>'; 
        }?>
    <?php  endwhile; ?> 
<?php endif;  ?>

Ответы [ 2 ]

0 голосов
/ 06 февраля 2020

Вам нужно получить последний срок и использовать tax_query

   $args_query = array(
    'post_type' => array('rationale'),
    'paged' => $paged,
    'posts_per_page' => -1,
);

$terms = get_terms(array(
    'taxonomy' => 'post_tag',
    'hide_empty' => false,
));

if (!empty($terms) && is_array($terms)) {
    $args_query['tax_query'] => array(
        array(
            'taxonomy' => 'company_list',
            'field' => 'term_id',
            'terms' => array($terms['0']->term_id), // single or array with id's
        ),
    ),
}

$query = new WP_Query($args_query);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        $terms = get_the_terms($post->ID, 'company_list');
        foreach ($terms as $term) {
            $termlinks = get_term_link($term);
            echo '<p class="post-content--cat">';
            echo '<a href="' . $termlinks . '">' . $term->name . '</a>';
            echo '</p>';
        }
    }
} else {
    // no post found
}

wp_reset_postdata();
0 голосов
/ 06 февраля 2020

попробуйте

<code>$terms = get_terms( array(
    'taxonomy' => 'your taxonomy name',
    'hide_empty' => false,
    'orderby' => 'term_id',
    'order' => 'asc',
) );
foreach ($terms as $terms_row) {
    $terms_row->slug;
    echo "<pre>";
        print_r($terms_row);
    echo "
";}

Спасибо

...