To expand on the answer from @s_ha_dum – you need to modify a few things about your query.
First, use his suggested tax query:
$args = array(
'post_type' => 'custcpt',
'tax_query' => array(
array(
'taxonomy' => 'custtax',
'field' => 'id',
'terms' => $cat->term_id,
)
),
'order' => 'ASC'
);
Then, modify your code as follows:
$cat_posts = new WP_Query($args);
while ($cat_posts->have_posts()) :
$cat_posts->the_post();
// Below, when referencing the post variables, must be as $cat_post->post->ID, etc. ?>
<center><?php get_the_post_thumbnail($cat_post->post->ID, 'thumbnail'); ?></center>
<br />
<?php // Below, don't need to pass the post ID into these functions. ?>
<a href="https://wordpress.stackexchange.com/questions/119231/<?php get_permalink(); ?>"><?php get_the_title(); ?></a>
<br />
<br />
<?php endwhile; ?>
<?php endif; ?>
Note a few things:
- You must use
while ($cat_posts->have_posts)
to iterate over the
posts. - Call
cat_posts->the_post()
to prepare the data to be displayed in the functions such asget_the_title()
- When referencing post variables, such as ID, you must do so as
$cat_posts->post->ID
- When calling functions such as
get_the_title()
, you do not need to pass the ID in.