Title: Custom SELECT query (wpdb) pulls up same posts multiple times
Last modified: August 20, 2016

---

# Custom SELECT query (wpdb) pulls up same posts multiple times

 *  [Modifiedcontent](https://wordpress.org/support/users/modifiedcontent/)
 * (@modifiedcontent)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/custom-select-query-wpdb-pulls-up-same-posts-multiple-times/)
 * I’m trying to pull up custom posts (‘companies’) with this custom SELECT query
   code:
 * > global $wpdb, $post;
   > $results = $wpdb->get_results(“SELECT $wpdb->posts.*
   >  FROM $wpdb->posts, $wpdb-
   > >postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->posts.
   > post_status = ‘publish’ AND $wpdb->posts.post_type = ‘companies’ ORDER BY $
   > wpdb->posts.post_title ASC LIMIT 0, 20″);
   > if ( $results ):
   >  foreach ($results as $post) : setup_postdata( $post );
   > … etc.
 * It almost works, but each post is listed 4 to 7 times, apparently depending on
   how many times the title (company name) also appears in postmeta values.
 * So either the query is wrong or the foreach stuff is wrong. I can’t figure it
   out. It’s driving me nuts.
 * Can anyone spot the mistake and point me to a solution?
 * I need to use a regular SQL query, because I’m trying to integrate this with 
   other generic PHP scripts. query_posts etc. won’t work.

Viewing 1 replies (of 1 total)

 *  Thread Starter [Modifiedcontent](https://wordpress.org/support/users/modifiedcontent/)
 * (@modifiedcontent)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/custom-select-query-wpdb-pulls-up-same-posts-multiple-times/#post-2206049)
 * Nevermind. Found the solution:
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_
 * OBJECT_K?! WTF?!
 * Why did I need an obscure solution like that to make something this basic work?
   Or is there a more obvious solution?
 * The duplicates no longer show up, but they still mess up the post count (LIMIT).
   Can I define the LIMIT somewhere after OBJECT_K?
 * Should I use a query with get_row? How would that look? I’ve tried several variations,
   but haven’t been able to make it work.

Viewing 1 replies (of 1 total)

The topic ‘Custom SELECT query (wpdb) pulls up same posts multiple times’ is closed
to new replies.

## Tags

 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)
 * [wpdb](https://wordpress.org/support/topic-tag/wpdb/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Modifiedcontent](https://wordpress.org/support/users/modifiedcontent/)
 * Last activity: [14 years, 8 months ago](https://wordpress.org/support/topic/custom-select-query-wpdb-pulls-up-same-posts-multiple-times/#post-2206049)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
