Trying to get custom post of a custom taxonomy

I’m trying to get a link from a custom post with a custom taxonomy but I’m running into issues trying to get it. I’m wanting to send the user straight to the post if the taxonomy count is 1. If its greater than one post it goes to a page showing all the posts of the taxonomy. I have this second part working but I cant at the first bit to work, I cant get the taxonomy to return the post.

<?php

$taxonomy = ‘treatment_type’;
$terms = get_terms($taxonomy); // Get all terms of a taxonomy

//print_r($terms );

if ( $terms && !is_wp_error( $terms ) ) :?>

<?php foreach ( $terms as $term ) {

if(‘trending-treatment’ !== $term->slug && ‘skin-care’ !== $term->slug){ ?>

<?php if($term->count == 1){?>

<?php

$posts_array = get_posts(
array( ‘showposts’ => -1,
‘post_type’ => ‘treatment’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘treatment_type’,
‘field’ => ‘term_id’,
‘terms’ => $term->slug,
)
)
)
);
print_r( $posts_array );

?>

<h1>only 1</h1>
<?php print_r($term); ?>

<article class=”portfolio-item pf-rejuv”>

<div class=”portfolio-image”>
<a href=”POST LINK TO GO HERE”>
<img src=”<?php the_field(‘image’, $term); ?>” alt=”<?php echo $term->name; ?>”>
</a>
</div>

<div class=”portfolio-desc”>
<div class=”team-title”><h4><?php echo $term->name; ?></h4><span> <?php the_field(‘types_of_treatments’, $term); ?></span></div>
</div>

</article>

<?php }else{ ?>

<article class=”portfolio-item pf-rejuv”>

<div class=”portfolio-image”>
<a href=”<?php echo get_term_link($term->slug, $taxonomy); ?>”>
<img src=”<?php the_field(‘image’, $term); ?>” alt=”<?php echo $term->name; ?>”>
</a>
</div>

<div class=”portfolio-desc”>
<div class=”team-title”><h4><?php echo $term->name; ?></h4><span> <?php the_field(‘types_of_treatments’, $term); ?></span></div>
</div>

</article>

<?php } ?>

<?php }

} ?>

<?php endif;?>

Read more here:: Trying to get custom post of a custom taxonomy

Leave a Reply

Your email address will not be published. Required fields are marked *