Skip to content

Instantly share code, notes, and snippets.

@leoken
Created October 19, 2013 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoken/7061463 to your computer and use it in GitHub Desktop.
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
$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