Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save carlaizumibamford/61836b5bdf751a96b10835ca74c05162 to your computer and use it in GitHub Desktop.

Select an option

Save carlaizumibamford/61836b5bdf751a96b10835ca74c05162 to your computer and use it in GitHub Desktop.
Display posts that have one tag, using tag slug:
$query = new WP_Query( array( 'tag' => 'cooking' ) );
Display posts that have this tag, using tag id:
$query = new WP_Query( array( 'tag_id' => 13 ) );
Display posts that have “either” of these tags:
$query = new WP_Query( array( 'tag' => 'bread,baking' ) );
Display posts that have “all” of these tags:
$query = new WP_Query( array( 'tag' => 'bread+baking+recipe' ) );
Display posts that are tagged with both tag id 37 and tag id 47:
$query = new WP_Query( array( 'tag__and' => array( 37, 47 ) ) );
To display posts from either tag id 37 or 47, you could use tag as mentioned above, or explicitly specify by using tag__in:
$query = new WP_Query( array( 'tag__in' => array( 37, 47 ) ) );
Display posts that do not have any of the two tag ids 37 and 47:
$query = new WP_Query( array( 'tag__not_in' => array( 37, 47 ) ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment