title: Make my MySQL query less verbose
body: <p>The following query was a real challenge for me to build for a Wordpress site using the wordpress wp_post table and the wp_postmeta table and the wp_user table. It works perfectly but it contains a lot of repeated statements. I am not sure how to simplify this (or if it can be). Any tips for simplifying and ridding the repetition would be appreciated.</p>
  
  <p>The tables contain data like this:</p>
  
  <pre><code>wp_posts
   ID | post_author | post_parent | post_type    | post_title | post_date
  2258  163           0             fep_message    a
  2262  1             2258          fep_message    re:a
  2264  163           2258          fep_message    re:a
  1698  1             0             fep_message    b
  1692  1             0             fep_message    c
  
  wp_postmeta
  meta_id | post_id | meta_key          | meta_value
  14696     2258      _fep_participants   1
  14697     2258      _fep_participants   163
  9819      1698      _fep_participants   163
  9820      1698      _fep_participants   1
  9759      1692      _fep_participants   163
  9760      1692      _fep_participants   1
  9815      1692      _fep_delete_by_1    1499496054
  13751     1698      _fep_delete_by_163  1501044119
  
  wp_user
  ID   | user_login 
  1      myname
  163    theirname
  </code></pre>
  
  <p>This is the query</p>
  
  <pre><code>SELECT a.*
  FROM (
  SELECT p.id, p.post_date, p.post_title, uf.user_login AS from_login, ut.user_login AS to_login 
  FROM wp_posts AS p
      JOIN wp_postmeta pm_to
      ON  (p.id = pm_to.post_id 
      AND pm_to.meta_key = '_fep_participants' 
      AND p.post_parent = 0 
      AND pm_to.meta_value &lt;&gt; p.post_author)
      JOIN wp_postmeta pm_delete
      ON (p.id = pm_delete.post_id)
  LEFT JOIN wp_users AS uf ON uf.ID = p.post_author 
  LEFT JOIN wp_users AS ut ON ut.ID = pm_to.meta_value    
  WHERE (p.post_type = 'fep_message' AND pm_delete.meta_value &lt;&gt; '_fep_delete_by_1' AND uf.user_login = 'myname'
  OR p.post_type = 'fep_message' AND pm_delete.meta_value &lt;&gt; '_fep_delete_by_1' AND ut.user_login = 'myname')
  AND p.post_date &gt;= '2017-07-07 00:00:00'  
  AND p.post_date &lt;= '2017-08-12 23:59:59'
  ORDER BY p.post_date
  ) a
  
  UNION
  
  SELECT b.*
  FROM (
  SELECT p.id, p.post_date, p.post_title, uf.user_login AS from_login, ut.user_login AS to_login 
  FROM wp_posts AS p
      JOIN wp_postmeta pm_to
      ON (p.post_parent = pm_to.post_id 
      AND p.post_parent &lt;&gt; 0 
      AND pm_to.meta_key = '_fep_participants' 
      AND pm_to.meta_value &lt;&gt; p.post_author) 
      JOIN wp_postmeta pm_delete
      ON (p.id = pm_delete.post_id)
  LEFT JOIN wp_users AS uf ON uf.ID = p.post_author 
  LEFT JOIN wp_users AS ut ON ut.ID = pm_to.meta_value    
  WHERE (p.post_type = 'fep_message' AND pm_delete.meta_value &lt;&gt; '_fep_delete_by_1' AND uf.user_login = 'myname'
  OR p.post_type = 'fep_message' AND pm_delete.meta_value &lt;&gt; '_fep_delete_by_1' AND ut.user_login = 'myname')
  AND p.post_date &gt;= '2017-07-07 00:00:00'  
  AND p.post_date &lt;= '2017-08-12 23:59:59'
  ORDER BY p.post_date
  ) b
  </code></pre>
  
  <p>The result it produces is:</p>
  
  <pre><code>id     |post_date              |post_title     |from_login     |to_login    
  1692    2017-07-07 11:45:03     c               myname          theirname   
  1698    2017-07-08 16:28:18     b               myname          theirname   
  2258    2017-08-11 23:15:10     a               theirname       myname  
  2262    2017-08-12 16:48:05     re:a            myname          theirname   
  </code></pre>