【问题标题】:Get posts that matches both taxonomies获取与两种分类法相匹配的帖子
【发布时间】:2010-11-15 17:12:01
【问题描述】:

我有一个电影数据库,我想知道演员 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 会搜索演员 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);

一切顺利, 貂

【问题讨论】:

    标签: php wordpress taxonomy


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 谢谢,但仍然没有任何查询。这是来自 print_r($loop); 的 sn-p; WP_Query 对象([query_vars] => 数组([post_type] => 电影 [posts_per_page] => -1 => [actors] => 数组([0] => A [1] => B)[错误] => [m] => 0 [p] => 0 [post_parent] =>
    【解决方案2】:

    在进行了一些挖掘和试验之后,找到了一种正确的方法。它涉及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 子句添加到循环中。我在自己的网站上测试了类似的代码,但如果它不起作用,请告诉我。

    【讨论】:

      猜你喜欢
      • 2017-10-11
      • 1970-01-01
      • 2015-05-02
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 2013-06-06
      相关资源
      最近更新 更多