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.
The topic ‘Custom SELECT query (wpdb) pulls up same posts multiple times’ is closed to new replies.