WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to have a related loop of posts on single post pages and to do this I want to search the tags of the post. My first question is can you use the search in query posts for multiple terms and what is correct way to do this. This is what I've been trying that is not working.

global $post;
$tags = wp_get_post_tags($post->ID);
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args = array('cat' => '8', 'posts_per_page' => 8, 's' => $tag_ids,);
$my_search = new WP_Query($args);
if ($my_search->have_posts()) : while ($my_search->have_posts()) : $my_search-  >the_post(); ?>   
share|improve this question
up vote 0 down vote accepted

If you are inside the loop, on the single page, than you have all tags and can search for releated posts with the same tags. You find many solutions via google; an example:

//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags( get_the_ID() );
if ( $tags ) {

  echo 'Related Posts';

  $first_tag = $tags[0]->term_id;
  $args = array(
    'tag__in'          => array( $first_tag ),
    'post__not_in'     => array( get_the_ID() ),
    'showposts'        => 5,
    'caller_get_posts' => 1

  $my_query = new WP_Query( $args );
  if ( $my_query->have_posts() ) {

    while ($my_query->have_posts()) : $my_query->the_post(); ?>

      <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>

share|improve this answer
tag_in searches for other posts with the same tag though. I want to search the tags as terms. – Pollux Khafra May 9 '12 at 11:57
Can you clarify this; i dont understand it. You have the tags of the single post - $tags = wp_get_post_tags( get_the_ID() ); and now you will use this tags for search as term, for what. What is your goal. Sorry, my english is not so fine and i hope you can give more information. – bueltge May 10 '12 at 5:26
People post to my site from the front end with gravity forms. Not everyone tags. Not everyone tags the same way. I'm not using suggested tags or anything like that so it gives me a better loop of related posts if I search the tags as terms. – Pollux Khafra May 10 '12 at 11:49
maybe you create the related posts about the content of posts? I have also an solution for this. I index the content and use the important verbs inside this and search relevant content to this words. – bueltge May 10 '12 at 17:43
Most of the content is just a video or mp3 without text so that won't work either. – Pollux Khafra May 10 '12 at 21:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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