Title: Custom post order when using OR relation
Last modified: August 30, 2016

---

# Custom post order when using OR relation

 *  [ejg](https://wordpress.org/support/users/ejg/)
 * (@ejg)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/custom-post-order-when-using-or-relation/)
 * I have a custom post type named Faculty. Faculty must be one or more of the type:
   MCT, MLT, or MT.
 *     ```
       MCT -> A, B
           MLT -> C, E
           MT  -> C, D
       ```
   
 * I want to list all MLT and MT faculty on a page in order by last name -> C, D,
   E
 * I am having a problem with the OR relation when used with orderby where it is
   returning all faculty that are MLT or MT or have a last name (which is everyone).
   The AND relation works correctly, returning just C.
 * My WP_Query arguments are:
 *     ```
       Array(
               [post_type] => faculty
               [order] => ASC
               [orderby] => meta_value
               [meta_key] => faculty_last_name
               [meta_query] => Array(
                       [relation] => OR
                       [0] => Array(
                               [key] => faculty_MLT
                               [value] => MLT
                               [compare] => =
                           )
                       [1] => Array(
                               [key] => faculty_MT
                               [value] => MT
                               [compare] => =
                           )
                   )
           )
       ```
   
 * The SQL created is:
 *     ```
       SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta
           ON wp_posts.ID = wp_postmeta.post_id INNER JOIN wp_postmeta AS mt1
           ON (wp_posts.ID = mt1.post_id)
           INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id)
           WHERE 1=1 AND wp_posts.post_type = 'faculty' AND (wp_posts.post_status = 'publish'
           OR wp_posts.post_status = 'private')
           AND (wp_postmeta.meta_key = 'faculty_last_name'
           -->OR (mt1.meta_key = 'faculty_MLT' AND CAST(mt1.meta_value AS CHAR) = 'MLT')
           OR (mt2.meta_key = 'faculty_MT' AND CAST(mt2.meta_value AS CHAR) = 'MT') )
           GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value ASC LIMIT 0, 10
       ```
   
 * If I change the –>OR to AND, the query returns the correct result.
 * Is there some way to use orderby along with relation => OR in wp_query?

The topic ‘Custom post order when using OR relation’ is closed to new replies.

## Tags

 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)
 * [wp-query](https://wordpress.org/support/topic-tag/wp-query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [ejg](https://wordpress.org/support/users/ejg/)
 * Last activity: [10 years, 7 months ago](https://wordpress.org/support/topic/custom-post-order-when-using-or-relation/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
