Need help with complex SQL query
-
Hello,
I’m currently working on a WordPress site with a custom post type ‘note’ that has a custom hierarchical taxonomy called ‘note_category’. Each post of type ‘note’ has a custom field called ‘display_priority’.
On the front page of the site, all posts of type ‘note’ having a ‘display_priority’ value of 1 are listed in date order followed by all other notes in date order. I achieved this using the following custom SQL query string, which seems to work fine.
$querystr = " (SELECT $wpdb->posts.* FROM $wpdb->posts, $wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = 'display_priority' AND $wpdb->postmeta.meta_value = '1' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'note' ORDER BY $wpdb->posts.post_date DESC) UNION ALL (SELECT $wpdb->posts.* FROM $wpdb->posts, $wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = 'display_priority' AND $wpdb->postmeta.meta_value != '1' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'note' ORDER BY $wpdb->posts.post_date DESC)";Now for my problem: I need to modify the above query to only return entries beloning to a given ‘note_category’. How would one go about this?
I’m pretty new to both SQL and the inner workings of WordPress so any help would be greatly appreciated.
The topic ‘Need help with complex SQL query’ is closed to new replies.