5

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?

8
  • codex.wordpress.org/Function_Reference/get_term_by
    – inarilo
    Commented Jun 6, 2017 at 15:13
  • @inarilo I'm a little confused how to use that in this particular context?
    – bhood
    Commented Jun 6, 2017 at 15:16
  • use the slug to get the taxonomy details. you don't need the loop for that.
    – inarilo
    Commented Jun 6, 2017 at 15:21
  • or maybe i didn't understand your question
    – inarilo
    Commented Jun 6, 2017 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
    Commented Jun 6, 2017 at 15:25

2 Answers 2

6

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()

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.