Php 获取与这两种分类法匹配的帖子
我有一个电影数据库,我想知道演员a和演员B都在哪部电影中出现过Php 获取与这两种分类法匹配的帖子,php,wordpress,taxonomy,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-
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;
}
祝你一切顺利,
Marten试试这个:
'actors' => array('A', 'B')
在做了一些挖掘和实验后,找到了正确的方法。它包括posts\u join和posts\u,其中过滤器:
$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;
}
$actor1='A';
$actor2='B';
函数join\u it($join){
$join=“内部连接$wpdb->term\u关系tr1 ON($wpdb->posts.ID=tr1.object\u ID)
内部连接$wpdb->term\u taxonomy tt1 ON(tr1.term\u taxonomy\u id=tt1.term\u taxonomy\u id)
内部连接$wpdb->terms t1 ON(tt1.term\u id=t1.term\u id)
内部联接$wpdb->term\u关系tr2 ON($wpdb->posts.ID=tr2.object\u ID)
内部连接$wpdb->term\u taxonomy tt2 ON(tr2.term\u taxonomy\u id=tt2.term\u taxonomy\u id)
内部连接$wpdb->terms t2 ON(tt2.term_id=t2.term_id)”;
返回$join;
}
函数where\u it($where){
全球1美元;
全球$2;
$where=“where$wpdb->posts.post\u type='movies'
和tt1.taxonomy='actors'
tt2.taxonomy='actors'
和t1.slug={$actor1}
和t2.slug={$actor2}”;
}
函数getmoviefromactor(){
全局$wp_查询;
全球$wpdb;
全球$员额;
添加过滤器('posts\u join','join\u it',10);
添加过滤器('posts\u where','where\u it',10);
$loop=新的WP_查询();
移除过滤器('posts\u join','join\u it',10);
移除过滤器('posts\u where','where\u it',10);
打印(循环);
而($loop->have_posts()):$loop->the_post();
?>
谢谢,但它仍然没有查询任何内容。这是来自print\u r($loop);WP\u查询对象([query\u vars]=>Array([post\u type]=>movies[posts\u per\u page]=>-1=>[actors]=>Array([0]=>a[1]=>B)[error]=>[m]=>0[p]=>0[post\u parent]=>
$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;
}