Ordering posts by Custom Field
-
Hi!
I am attempting to build a custom query that will allow wordpress to function as an event calender. I hope that in the future I can turn it into a plugin and release it to the community as a whole. Let me describe it.I want my event calender page to return a list of all events in a category whose name is equal to the current month and year. This is working fine. I then want it to sort the posts based on the custom field eventdate1 (the first date of an engagement) and this is where it doesn’t seem to work. Here is the custom query I am using:
$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 = '$cat1' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->posts.post_status = 'publish' AND $wpdb->postmeta.meta_key = 'eventdate1' ORDER BY $wpdb->postmeta.meta_value ASC "; $pageposts = $wpdb->get_results($querystr, OBJECT);
Is there a better way to order by a custom field?
Here is a link to all of the code if you want to see it
https://wordpress.pastebin.ca/985355
- The topic ‘Ordering posts by Custom Field’ is closed to new replies.