I’ve created a custom post type called 'publications'
and a custom taxonomy called 'topics'
. I’m also using the standard taxonomy of 'category'
.
My Custom Query makes sure that it fetches ALL 'publications'
that are in the correct 'category'
but I’d like it to also ORDER BY
the additional 'topics'
taxonomy.
This custom query does fetch all the correct 'publications'
but I’m having no luck with the ORDER BY
section:
$querystr = "
SELECT *
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_type="publications"
AND $wpdb->terms.slug = %s
AND $wpdb->term_taxonomy.taxonomy = 'category'
ORDER BY $wpdb->term_taxonomy.taxonomy = 'topics' DESC
";
$pageposts = $wpdb->get_results($wpdb->prepare($querystr, array($parent_post_slug)));
The $parent_post_slug
is the 'category'
name. And it’s fetching all the correct Posts. Just how would I order them by the taxonomy called 'topics'
?
An example of the order I’d like:
Category Name = Fiction (This page is just showing the fiction publications)
Publication 1 = has custom taxonomy topic of Alligators
Publication 2 = has custom taxonomy topic of Alligators
Publication 3 = has custom taxonomy topic of Antelopes
Publication 4 = has custom taxonomy topic of Buffalos
Publication 5 = has custom taxonomy topic of Buffalos
Any idea of what I should be using in the ORDER BY
line to get this to work?