Title: Query string which does not work
Last modified: August 21, 2016

---

# Query string which does not work

 *  Resolved [Losmi](https://wordpress.org/support/users/uloszaminje/)
 * (@uloszaminje)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/query-string-which-does-not-work/)
 * I need to list posts starting with letter ‘$letter’:
 *     ```
       $results = $wpdb->get_results("SELECT * FROM wp_posts p
       	JOIN wp_term_relationships r ON r.object_id = p.ID
       	JOIN wp_terms terms ON terms.term_id = t.term_id
       	WHERE terms.name IN ('cat1', 'cat2', 'cat3')
       	AND p.post_status = 'publish'
       	AND p.post_title LIKE '$letter%' ");
       ```
   
 * This query returns NOTHING. When I remove the POST TITLE LIKE… clause, it returns
   what it should, but I do need that clause because I just want posts that begin
   with given letter.
 * What is wrong with my query string?

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/query-string-which-does-not-work/#post-4992697)
 * It looks like part of the query is missing from your example. ‘t.term_id’ would
   be undefined.
 *  Thread Starter [Losmi](https://wordpress.org/support/users/uloszaminje/)
 * (@uloszaminje)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/query-string-which-does-not-work/#post-4992739)
 * sorry, here:
 *     ```
       $results = $wpdb->get_results("SELECT * FROM wp_posts p
       	JOIN wp_term_relationships r ON r.object_id = p.ID
       	JOIN wp_terms terms ON terms.term_id = r.term_taxonomy_id
       	WHERE terms.name IN ('cat1', 'cat2', 'cat3')
       	AND p.post_status = 'publish'
       	AND p.post_title LIKE '$letter%' ");
       ```
   
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/query-string-which-does-not-work/#post-4992745)
 * Sometimes it helps to set up the SQL separately and echo it out to see what is
   being created. Try something like this:
 *     ```
       $sql = "SELECT p.*
              FROM $wpdb->posts p
              JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id
              JOIN $wpdb->term_taxonomy tt ON ( tr.term_taxonomy_id = tt.term_taxonomy_id
                     AND tt.taxonomy = 'category' )
              JOIN $wpdb->terms t ON ( tt.term_id = t.term_id AND t.name IN ( 'cat1', 'cat2', 'cat3' ) )
              WHERE p.post_status = 'publish'
              AND p.post_title LIKE '$letter%'
              ";
       echo "<p>SQL: $sql</p>";
       $results = $wpdb->get_results($sql);
       ```
   
 *  Thread Starter [Losmi](https://wordpress.org/support/users/uloszaminje/)
 * (@uloszaminje)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/query-string-which-does-not-work/#post-4992754)
 * Damnit, I just realized that I spent the whole day yesterday trying to select
   all articles starting with ‘A’ and there were actually none, I just had to change
   this test letter to some other… My SQL was fine all along!!!#!&#%^*#(&)@$%(^&
 * Thank you vtxyzzy very much for your quick help, your SQL looks much more legit
   to me than mine, I adopted it 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Query string which does not work’ is closed to new replies.

## Tags

 * [post title](https://wordpress.org/support/topic-tag/post-title/)
 * [Query String](https://wordpress.org/support/topic-tag/query-string/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [Losmi](https://wordpress.org/support/users/uloszaminje/)
 * Last activity: [11 years, 11 months ago](https://wordpress.org/support/topic/query-string-which-does-not-work/#post-4992754)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
