Trying to get custom post of a custom taxonomy

I can’t get the first bit to work, I can’t get the taxonomy to return
the post.

So the following is the first bit which queries posts in a single taxonomy term:

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

And the problem there is that the tax_query‘s field and terms values don’t match — you set the field to term_id (term ID), but then the terms is a term slug and not ID.

So make sure they match:

  • Change the 'field' => 'term_id' to 'field' => 'slug'.

  • Or change the 'terms' => $term->slug to 'terms' => $term->term_id.

And actually, you should use the parameter numberposts or better posts_per_page and not showposts (which is not a standard parameter for get_posts() or WP_Query).