title: Wordpress Filter Posts by Tags
body: <p>I am trying to filter posts by tags.</p>
  
  <p>Posts with all tags are available by default.
  But a user can increase and decrease the number of tags in the filter.</p>
  
  <p>What is the best way to filer post that way?
  Should I count all omitted tags and work with URL arguments or is there an easier way?</p>
  
  <pre><code>$args  =
          array(
               'tag'        =&gt; $tags_arr, //array('sport', 'art', 'news')
           );
  $posts_by_tags = get_posts( $args );
  foreach ( $posts_by_tags as $post_by_tags ){
    echo '&lt;li&gt;' . $post_by_tags-&gt;post_title . '&lt;/li&gt;';
  }
  </code></pre>
  
  <p><strong>UPD:</strong></p>
  
  <p>I decided to make links like:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;ul&gt;
   &lt;li&gt;&lt;a href="?tags=apples,oranges"&gt;Bananas&lt;/a&gt;&lt;/li&gt;
   &lt;li&gt;&lt;a href="?tags=bananas,oranges"&gt;Apples&lt;/a&gt;&lt;/li&gt;
   &lt;li&gt;&lt;a href="?tags=apples,bananas"&gt;Oranges&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>PHP:</p>
  
  <pre><code>$tags = array();
  foreach ( get_tags() as $before_tag ) {
      $tags[] = $before_tag-&gt;name;
  }
  if ( isset( $_REQUEST['tags'] ) &amp;&amp; $_REQUEST['tags'] != '' &amp;&amp; $_REQUEST['tags'] != null ) {
      $filter_tags = explode( ',', $_REQUEST['tags'] );
  } else {
      $filter_tags = $tags;
  }
  $args =
      array(
          'tag'         =&gt; $filter_tags,
      );
  $posts_by_tags = get_posts( $args );
  $each_sixth    = 1;
  foreach ( $posts_by_tags as $post_by_tags ){
      // do stuff
      echo get_the_title( $post_by_tags-&gt;ID );
  }
  </code></pre>