Title: Help Needed Retrieving Posts With SQL
Last modified: August 19, 2016

---

# Help Needed Retrieving Posts With SQL

 *  [stevewilson](https://wordpress.org/support/users/stevewilson/)
 * (@stevewilson)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/help-needed-retrieving-posts-with-sql/)
 * I want to isolate certain posts within a category using a custom taxonomy (‘trans-
   front-001’ in this instance). Dealing with the category comes later, but first
   the following. The nested subquery works — it delivers the IDs of desired posts—
   but the SQL that wraps it that is meant to produce the posts themselves seems
   to yield nothing:
 *     ```
       <?php
       $query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID =
       (SELECT object_id FROM wp_terms, wp_term_relationships
       WHERE wp_terms.term_id = wp_term_relationships.term_taxonomy_id
       AND wp_terms.slug='trans-front-001')");
   
       $whichposts = $wpdb->get_results( $query, OBJECT );
       ?>
       ```
   
 * Or is the code to which I then deliver these posts a mismatch (working code that
   was cannibalized from elsehwere — reduced here for concision)):
 *     ```
       <?php
       if ($whichposts):
          global $post;
          foreach ($whichposts as $post):
             setup_postdata($post); ?>
             <div class="post" id="post-<?php the_ID(); ?>">
                <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
                <?php the_title(); ?></a></h2>
             </div>
          <?php endforeach; ?>
        <?php endif; ?>
       ```
   

The topic ‘Help Needed Retrieving Posts With SQL’ is closed to new replies.

## Tags

 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [stevewilson](https://wordpress.org/support/users/stevewilson/)
 * Last activity: [15 years, 2 months ago](https://wordpress.org/support/topic/help-needed-retrieving-posts-with-sql/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
