title: Custom Query with custom post types and category id
body: <p>I am trying to grab a list of posts by a specific post type and the category id that it is assigned too.</p>
  
  <p>So far from searching online i have managed to come up with</p>
  
  <p><code></p>
  
  <pre><code>function fs_tpl_get_results($post_type, $mycat) {
      global $wpdb;
  
  
      $args = wp_parse_args($args,array(
      'post_type'        =&gt; '$post_type',
      'term_id'          =&gt; 22,
    ));
    extract($args);
  
  
      $sql = &lt;&lt;&lt;SQL
  SELECT DISTINCT
    {$wpdb-&gt;terms}.*,
    COUNT(*) AS post_count
  FROM
    {$wpdb-&gt;terms}
    INNER JOIN {$wpdb-&gt;term_taxonomy} ON {$wpdb-&gt;terms}.term_id={$wpdb-&gt;term_taxonomy}.term_id
    INNER JOIN {$wpdb-&gt;term_relationships} ON {$wpdb-&gt;term_taxonomy}.term_taxonomy_id={$wpdb-&gt;term_relationships}.term_taxonomy_id
    INNER JOIN {$wpdb-&gt;posts} ON {$wpdb-&gt;term_relationships}.object_id={$wpdb-&gt;posts}.ID
    INNER JOIN {$wpdb-&gt;term_relationships} related_relationship ON {$wpdb-&gt;posts}.ID=related_relationship.object_id
    INNER JOIN {$wpdb-&gt;term_taxonomy} related_term_taxonomy ON related_relationship.term_taxonomy_id=related_term_taxonomy.term_taxonomy_id
    INNER JOIN {$wpdb-&gt;terms} related_terms ON related_term_taxonomy.term_id=related_terms.term_id
  WHERE 1=1
    AND (related_term_taxonomy.taxonomy&lt;&gt;{$wpdb-&gt;term_taxonomy}.taxonomy OR related_terms.term_id&lt;&gt;{$wpdb-&gt;terms}.term_id)
    AND {$wpdb-&gt;posts}.post_type='%s'
    AND related_terms.term_id=%d
  GROUP BY
    {$wpdb-&gt;terms}.term_id
  SQL;
    $sql = $wpdb-&gt;prepare($sql,$post_type,$term_id);
  
  
  
      $results=$wpdb-&gt;get_results($sql);
  
      return $results;
  }
  </code></pre>
  
  <p></code></p>
  
  <p>But it returns an empty result, does anyone have any ideas to fix this please?</p>