You had to use LEFT JOIN
to simplify the stuff. By the way, the same can be accomplished using WP_Query
and I actualy don’t understand why you don’t want to use it.
Also I’ve changed the way you write the query and I’ve not use the hardcoded $table_prefix
for answer to be useful for others too. You can replace $wpdb->
with wp_
in $query
if you want to.
I assume you have third category ID=1000
(see NOT IN
part):
<?php
global $wpdb;
$query = "
SELECT ID, post_title
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON (ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id IN(15, 5)
AND $wpdb->term_taxonomy.term_id NOT IN(1000)
ORDER BY ID DESC
LIMIT 0, 60
";
$results = $wpdb->get_results($query);
foreach($results as $result) {
echo $result->ID;
echo '<br />';
echo $result->post_title;
echo '<hr />';
}