Title: killiantobin's Replies | WordPress.org

---

# killiantobin

  [  ](https://wordpress.org/support/users/killiantobin/)

 *   [Profile](https://wordpress.org/support/users/killiantobin/)
 *   [Topics Started](https://wordpress.org/support/users/killiantobin/topics/)
 *   [Replies Created](https://wordpress.org/support/users/killiantobin/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/killiantobin/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/killiantobin/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/killiantobin/engagements/)
 *   [Favorites](https://wordpress.org/support/users/killiantobin/favorites/)

 Search replies:

## Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)

 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [help? – Custom SQL Query. How to ORDER BY two fields?](https://wordpress.org/support/topic/help-custom-sql-query-how-to-order-by-two-fields/)
 *  [killiantobin](https://wordpress.org/support/users/killiantobin/)
 * (@killiantobin)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/help-custom-sql-query-how-to-order-by-two-fields/#post-711249)
 * Nevermind!
    I think I fixed it by removing the line:
 * `LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.
   object_id)`
 * It seems happy!
 *   Forum: [Plugins](https://wordpress.org/support/forum/plugins-and-hacks/)
    In
   reply to: [help? – Custom SQL Query. How to ORDER BY two fields?](https://wordpress.org/support/topic/help-custom-sql-query-how-to-order-by-two-fields/)
 *  [killiantobin](https://wordpress.org/support/users/killiantobin/)
 * (@killiantobin)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/help-custom-sql-query-how-to-order-by-two-fields/#post-711248)
 * Thanks for the help so far, this thread has been very helpful.
    I am also trying
   to use WP as an event listing. I have two custom fields I want to use to filter
   and sort the postings: startdate and enddate.
 * The filtering seems to work and so does the sorting, but **I am getting 3 of 
   each posting returned when I run the loop.**
 * Can anyone point out where I am going wrong here?
 * Thanks!
 *     ```
       <?php 
   
       	$querystr = "
       		SELECT * FROM $wpdb->posts
       		LEFT JOIN $wpdb->postmeta AS startdate ON(
       		$wpdb->posts.ID = startdate.post_id
       		AND startdate.meta_key = 'startdate'
                       )
       		LEFT JOIN $wpdb->postmeta AS enddate ON(
       		$wpdb->posts.ID = enddate.post_id
       		AND enddate.meta_key = 'enddate'
       		)
       		LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
       		WHERE $wpdb->posts.post_status = 'publish'
       		AND enddate.meta_value > CURDATE()
                       AND startdate.meta_value <= DATE_ADD(CURDATE(),INTERVAL 30 DAY)
                       ORDER BY enddate.meta_value, startdate.meta_value ASC
       	";
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)