On my WordPress site I have three categories 'moviestar', 'interviews', and 'movies'.

On a 'moviestar' category page that is entitled 'Angelina Jolie' (and with a custom variable starname='Angelina Jolie'), I want to display posts from other categories tagged with 'Angelina Jolie'.

My Page Example:

Angelina Jolie

(normal page information)


query 1 - display linked list of all posts in 'interviews' category tagged with 'Angelina Jolie'


query 2 - display linked list of all posts in 'movies' category tagged with 'Angelina Jolie'

NextGen Gallery

query 3 - display all pictures in NextGen Gallery tagged with 'Angelina Jolie'

So my questions are:

  1. How do I query posts in specific categories tagged with Angelina Jolie? Like this?

    global $wpdb;
    $yourCategory = '123';
    $yourTag = 'Angelina-Jolie';
    $querystr = "
                SELECT p.* from $wpdb->posts p, $wpdb->terms t, $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->terms t2, $wpdb->term_taxonomy tt2, $wpdb->term_relationships tr2
                WHERE p.id = tr.object_id
                AND t.term_id = tt.term_id
                AND tr.term_taxonomy_id = tt.term_taxonomy_id
                AND p.id = tr2.object_id
                AND t2.term_id = tt2.term_id
                AND tr2.term_taxonomy_id = tt2.term_taxonomy_id
                AND (tt.taxonomy = 'category' AND tt.term_id = t.term_id AND t.slug = '$yourCategory')
                AND (tt2.taxonomy = 'post_tag' AND tt2.term_id = t2.term_id AND t2.slug = '$yourTag')
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    if ($pageposts):
        foreach ($pageposts as $post):
            // do regular WordPress Loop stuff in here
    else :
        // nothing found
  2. How do I modify the above to query multiple categories and store which category the post is in?

  3. Does it make sense that I will query once on the page and then use a PHP loop to echo out the information under each heading? Something like...

      echo "<li><a href='".$row['url']."'>". $row['post_title'] . "</a></li>";
  4. Would it be better to abandon using WordPress functions and queries and instead query the db directly using normal php?

I really need some advice or a good example. I'm using Wordpress 3.3.2

I have read the following but I just don't get it yet:

http://www.borishoekmeijer.nl/show-related-posts-using-custom-taxonomy/ https://stackoverflow.com/questions/8563136/wordpress-query-filtering-by-custom-field-tag-and-category

  • Is there a reason why your not actually using a default WordPress function, aka WP Query.
    – Wyck
    May 6, 2012 at 0:06

1 Answer 1


WP_Query can handle all of these cases. See the section on handling multiple taxonomies.

$this_cat = 'interviews';
$this_tag = 'angelina-jolie';

$args = array(
    'tax_query' => array(
        'relation' => 'AND',
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => $this_cat
            'taxonomy' => 'post_tag',
            'field' => 'slug',
            'terms' => $this_tag
$interviews = new WP_Query( $args );

while( $interviews->have_posts() ):
    // normal loops stuff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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