Created
October 19, 2013 20:58
-
-
Save leoken/7061463 to your computer and use it in GitHub Desktop.
Query by Category Name and Custom Field Key Sorted by Key's Value
This example selects posts that have a common category name and custom field key, then sorts the selection by the custom key's value. From http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$querystr = " | |
SELECT * FROM $wpdb->posts | |
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) | |
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->terms.name = 'slides' | |
AND $wpdb->term_taxonomy.taxonomy = 'category' | |
AND $wpdb->posts.post_status = 'publish' | |
AND $wpdb->posts.post_type = 'post' | |
AND $wpdb->postmeta.meta_key = 'order' | |
ORDER BY $wpdb->postmeta.meta_value ASC | |
"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment