I'm getting WordPress database error: [Unknown column 'wp_posts.ID' in 'field list'], but column does exist, can someone please tell me what I'm doing wrong?
SELECT * FROM wp_posts WHERE ID IN(
SELECT ID FROM
(
SELECT wp_posts.ID,wp_posts.post_date,d.name from wp_posts as a
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
JOIN wp_term_relationships AS b ON ( a.ID = b.object_id)
JOIN wp_term_taxonomy AS c ON (b.term_taxonomy_id = c.term_taxonomy_id)
JOIN wp_terms AS d ON (c.term_id = d.term_id)
WHERE c.taxonomy = 'my_custom_taxonomy'
GROUP BY d.name
HAVING (wp_posts.post_date = max(wp_posts.post_date))
)
tmp
);
wp_posts.ID
work? Is there a reason you didn't useWP_Query
to do this?WP_Query
can generate the SQL itself and draw on the various caches and optimisations, as well as handling those edge cases yours doesn't, e.g. post status, post passwords, post types, etc. Generally you shouldn't be writing SQL for the main posts table, the situations where it makes sense are exceptionally rarea.ID
since you aliaswp_posts AS a
?WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
below last select and now I'm getting this error'WHERE c.taxonomy = 'my_custom_taxonomy' GROUP BY d.name HAVING (' at line 10]
WHERE
clause.