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
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?
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: