Best practice for multiple queries on page

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)

Interviews

query 1 – display linked list of all posts in ‘interviews’ category tagged with ‘Angelina Jolie’

Filmography

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):
            setup_postdata($post);
            // do regular WordPress Loop stuff in here
        endforeach;
    else :
        // nothing found
    endif;
    
  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…

    while(($row=mysql_fetch_array($result))&&($row['cat']=='845')){
      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

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',
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => $this_cat
        ),
        array(
            'taxonomy' => 'post_tag',
            'field' => 'slug',
            'terms' => $this_tag
        )
    )
);
$interviews = new WP_Query( $args );

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

Leave a Comment