Retrieve posts by term id custom query

Have you tried using the WP_Query class? You might find it’s easier to use the built-in tools for this instead of a custom query from scratch. Something similar to the following should work for you:

<?php
$args = array(
'post_type' => 'recipe_cpt',
'tax_query' => array(
    array(
    'taxonomy' => 'recipe_tx',
    'field' => 'term_id',
    'terms' => 37
     )
  )
);
$query = new WP_Query( $args ); ?>

EDIT: note the tax_query is an array of arrays by design. Many tax query problems are a result of missing this detail.

EDIT: corrected field value typo above, replacing ‘id’ with ‘term_id’.

Leave a Comment