获取与两种分类法相匹配的帖子

时间:2010-11-15 17:12:01

标签: php wordpress taxonomy

我有一个电影数据库,我想知道演员A和B都有哪些电影。

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-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;
}

此代码存在的问题是,Wordpress默认情况下会搜索Actor A或B并显示他们所有的电影,而不仅仅是他们已经推荐过的电影。

谢谢, 貂


修改 我想即将到来,我陷入了一个SQL查询,如果我只是搜索其中一个演员,它会很有效,当我搜索这两个时问题就会产生,这会产生一个空数组。

当我在SQL查询中进行手动搜索时,我看到具有不同term.slugs的重复内容,是否有任何解决方法?

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);

一切顺利, 貂

2 个答案:

答案 0 :(得分:0)

试试这个:

'actors' => array('A', 'B')

答案 1 :(得分:0)

经过一些挖掘和实验,找到了正确的方法。它涉及posts_join和posts_where过滤器:

$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;
}

posts_where和posts_join过滤器分别将where和join子句添加到循环中。我在自己的网站上测试了类似的代码,但是如果它不起作用,请告诉我。

相关问题