SQL help

Soldato
Joined
26 Aug 2004
Posts
7,571
Location
London
I've been struggling with an sql query for hours - if anyone could help I'd be hugely grateful.

I'm trying to order wordpress posts by category. This is what I have so far

Code:
	SELECT DISTINCT *
	FROM $wpdb->posts as wpost
	INNER JOIN $wpdb->term_relationships
	ON (wpost.ID = $wpdb->term_relationships.object_id)
	INNER JOIN $wpdb->term_taxonomy
	ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
	INNER JOIN $wpdb->terms
	ON ($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id)
	AND $wpdb->term_taxonomy.taxonomy = 'category'
	AND $wpdb->term_taxonomy.term_id
	WHERE post_parent = '$thispost'
	AND wpost.post_status = 'publish'

 ";


This works, however, I want to order the posts by the category name. I've tried 'ORDER BY terms.name' it breaks as soon as I do that. Any ideas?
 
Last edited:
You sir are a genius (I'm avoiding the me being an idiot part)! I was so close! Thank you :D
 
Back
Top Bottom