title: Wordpress: get_posts filter
body: <p>I am trying to create some filters in my posts with custom fields.
  So in my <code>searchpage.php</code> I have added the following:</p>
  
  <pre><code>$args = array(
      'post_type'  =&gt; 'cp_course', 'numberposts' =&gt;-1,'orderby' =&gt; 'ID', 'order' =&gt; 'ASC', 's' =&gt; $searchterm,
      'meta_query' =&gt; array(
          'relation' =&gt; 'AND',
          array(
              'key'   =&gt; 'course_code',
              'value' =&gt; $code,
          ),
          array(
              'key' =&gt; 'course_duration',
              'value' =&gt; $duration,
          )
      )
  );
  
  $course = get_posts($args);
  </code></pre>
  
  <p>But it won't work if I supply only <code>$code</code> or only <code>$duration</code>, I have to supply them both due to the <code>AND</code> relation. Is there a simpler way than multiple <code>IFs</code> to check if for example <code>$code</code> is not supplied then filter posts only by <code>$duration</code></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>By using <a href="https://stackoverflow.com/a/52851353/10408255" title="ashour solution">ash0ur solution</a> I used this code:</p>
  
  <pre><code>$args = array(
      'post_type'  =&gt; 'cp_course', 'numberposts' =&gt;-1,'orderby' =&gt; 'ID', 'order' =&gt; 'ASC', 's' =&gt; $searchterm,
      'meta_query' =&gt; array(
          'relation' =&gt; 'OR',
          array(
              'relation' =&gt; 'AND',
              array(
                  'key'   =&gt; 'course_code',
                  'value' =&gt; $code,
              ),
              array(
                  'key' =&gt; 'course_duration',
                  'value' =&gt; $duration,
              ),          
          ),
          array(
          'key'   =&gt; 'course_duration',
          'value' =&gt; $duration,
          ),
          array(
          'key'   =&gt; 'course_code',
          'value' =&gt; $code,
          )
      )
  );
  </code></pre>
  
  <p>But if I want to extend it to add more search terms I get to many <code>INNER JOIN</code> queries in the database which causes problem to the server. Here is what I ve tried which causes the issues:</p>
  
  <pre><code>$args = array(
      'post_type'  =&gt; 'cp_course', 'numberposts' =&gt;-1,'orderby' =&gt; 'ID', 'order' =&gt; 'ASC', 's' =&gt; $searchterm,
      'meta_query' =&gt; array(
          'relation' =&gt; 'OR',
          array(
              'relation' =&gt; 'AND',
              array(
                  'key'   =&gt; 'course_code',
                  'value' =&gt; $code,
              ),
              array(
                  'key' =&gt; 'course_duration',
                  'value' =&gt; $duration,
              ),  
              array(
                  'key'   =&gt; 'course_iteration_start',
                  'value' =&gt; $date_from,
              ),
              array(
                  'key' =&gt; 'course_iteration_end',
                  'value' =&gt; $date_to,
              ),  
          ),
          array(
          'key'   =&gt; 'course_duration',
          'value' =&gt; $duration,
          ),
          array(
          'key'   =&gt; 'course_code',
          'value' =&gt; $code,
          )
      )
  );
  </code></pre>
  
  <p>Where <code>course_iteration_start</code> and <code>course_iteration_end</code> are <code>sub fields</code> (repeaters) of <code>course_iteration</code> <code>custom field</code>. </p>