Get posts that matches both taxonomies
I have a movie-database, i want to know which movies actor A and B has both been featured in.
function getmoviefromactor(){
global $wp_query;
global $wpdb;
global $post;
$loop = new WP_Query(array(
'post_type' => 'movies',
'actors' => 'A', 'B',
'posts_per_page' =>-1,
));
print_r($loop);
while ( $loop->have_posts() ) : $loop->the_post();
?>
<h2 class="entry-titl开发者_JS百科e"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<?php
the_content();
endwhile;
}
The problem with this code is that Wordpress by default is searching for Actor A or B and displaying every movie they've been featured in and not just the movie(s) they've both been featured in.
Thanks, Marten
EDIT: I think im almost there, im stuck in a SQL-query, it works perfect if i just search for one of the actors, the problem accors when i search for both, which results in an empty array.
When i do the manual search in the SQL query i see duplicate content with different term.slugs, is there any workaround for this?
global $wpdb;
$querystr = "
SELECT *
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_type = 'movies'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'actors'
AND $wpdb->terms.slug = 'A'
AND $wpdb->terms.slug = 'B'
ORDER BY $wpdb->posts.post_date DESC";
$pageposts = $wpdb->get_results($querystr, OBJECT);
print_r($pageposts);
All the best, Marten
try this:
'actors' => array('A', 'B')
After doing some digging and experimenting, found a way to do it right. It involves posts_join and posts_where filters:
$actor1 = 'A';
$actor2 = 'B';
function join_it($join) {
$join = " INNER JOIN $wpdb->term_relationships tr1 ON($wpdb->posts.ID = tr1.object_id)
INNER JOIN $wpdb->term_taxonomy tt1 ON(tr1.term_taxonomy_id = tt1.term_taxonomy_id)
INNER JOIN $wpdb->terms t1 ON(tt1.term_id = t1.term_id)
INNER JOIN $wpdb->term_relationships tr2 ON($wpdb->posts.ID = tr2.object_id)
INNER JOIN $wpdb->term_taxonomy tt2 ON(tr2.term_taxonomy_id = tt2.term_taxonomy_id)
INNER JOIN $wpdb->terms t2 ON(tt2.term_id = t2.term_id)";
return $join;
}
function where_it($where) {
global $actor1;
global $actor2;
$where = " WHERE $wpdb->posts.post_type = 'movies'
AND tt1.taxonomy = 'actors'
AND tt2.taxonomy = 'actors'
AND t1.slug = {$actor1}
AND t2.slug = {$actor2}";
}
function getmoviefromactor(){
global $wp_query;
global $wpdb;
global $post;
add_filter('posts_join','join_it',10);
add_filter('posts_where','where_it',10);
$loop = new WP_Query();
remove_filter('posts_join','join_it',10);
remove_filter('posts_where','where_it',10);
print_r($loop);
while ( $loop->have_posts() ) : $loop->the_post();
?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<?php
the_content();
endwhile;
}
The posts_where and posts_join filters adds where and join clauses respectively to the loop. I tested a similar code with my own site but if it doesn't work, please let me know.
精彩评论