0

The following code works fine for me:

$term = get_term_by('slug', ‘foo’, 'post_tag');       
$args = array ( 
  'posts_per_page' => 5, 
  'paged' => $paged,
  'post__not_in' => get_option( 'sticky_posts' ) ,
  'tag__not_in' => $term
);

I'd now like to get_term_by multiple tags, e.g., 'foo' and 'bar' (I'm trying to display all posts except those tagged 'foo' or 'bar').

I realize this is probably elementary, but I'm not sure how to go about it.

Ideas?

0

I've come up with a better way than in my previous answer:

First you need to set up an array containing the slugs of the tags you want to exclude. Then loop over that array, and on each term, use get_term_by() to get the tag, saving its ID to another array. That is what you can pass through to your query.

$tag_slugs = array(
    'foo',
    'bar',
);

$tag_ids = array();
foreach( $tag_slugs as $slug ) {
    $tag = get_term_by( 'slug', $slug, 'post_tag' );
    $tag_ids[] = $tag->term_id;
}

$args = array ( 
  'posts_per_page' => 5, 
  'paged'          => $paged,
  'post__not_in'   => get_option( 'sticky_posts' ),
  'tag__not_in'    => $tag_ids,
);
0

get_term_by() can only retrieve a single term. Try using get_terms() instead. http://codex.wordpress.org/Function_Reference/get_terms

  • Thanks for your speedy response, but even with get_terms, I'm still uncertain how to address multiple tags slugs. – MisterSpeck Aug 14 '14 at 19:45
  • I just realized that's not the best way... see my new answer below. :) – Peter Andrews Aug 14 '14 at 19:46
  • Thanks very much, Peter! This works great, and it's very understandable! – MisterSpeck Aug 14 '14 at 20:00
  • Great! Please don't forget to mark my answer above (the one you're referring to) as "accepted". :) – Peter Andrews Aug 14 '14 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.