4

I can't figure how to get the ID of the custom taxonomy I'm using to loop through the custom post type called "test_values".

function prefix_load_term_posts () {
    $term_slug = $_POST[ 'term' ];
    $args = array (
             'post_type' => 'test_values',
             'posts_per_page' => 1,
             'tax_query' => array(
              array(
                    'taxonomy' => 'test_value_category',
                    'field'    => 'slug',
                    'terms'    => $term_slug ,
               ),

              ),
         );

    global $post;

    $myposts = get_posts( $args );
    ob_start ();

    foreach( $myposts as $post ) : setup_postdata($post); ?>

    <?php endforeach; ?>

Anyone have any suggestions how to get this taxonomy ID within the loop?

  • @inarilo I'm a little confused how to use that in this particular context? – bhood Jun 6 '17 at 15:16
  • use the slug to get the taxonomy details. you don't need the loop for that. – inarilo Jun 6 '17 at 15:21
  • or maybe i didn't understand your question – inarilo Jun 6 '17 at 15:22
  • Yeah so I'm trying to get the ID for the custom taxonomy which is $post. When I echo `$post->term_id' within the foreach loop, nothing shows up... – bhood Jun 6 '17 at 15:25
5

You can try this function get_term_by($field, $value, $taxonomy, $output, $filter ) or

$termID = [];
$terms = get_the_terms($post->ID, 'taxonomy');
foreach ($terms as $term) {
    $termID[] = $term->term_id;
}

or get_queried_object_id()

|improve this answer|||||
1

I found the answer, what I had was way to complicated. Here's what I ended up doing that worked:

<?php $terms = get_the_terms( $post->ID, 'newsroom_post_category' ); 
                foreach($terms as $term) {
                    $termlinks = get_term_link($term);
                        echo '<p class="post-content--cat">';
                            echo '<a href="' . $termlinks . '">' . $term->name . '</a>';  
                        echo '</p>'; }?>

This got all the Taxonomy terms attributed to the custom post.

|improve this answer|||||

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.