2010-11-15 78 views
0

我有一個電影數據庫,我想知道哪個電影演員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查詢,它可以完美運行,如果我只是搜索的演員之一,這個問題accors當我搜索兩種,這將導致一個空數組。

當我在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); 

一切順利, 貂

回答

0

試試這個:

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

謝謝,但它仍然不是query'n什麼。這是來自print_r($ loop)的片段; WP_Query Object([query_vars] => Array([post_type] => movies [posts_per_page] => -1 => [actors] => Array([0] => A [1] => B)[error] => [m] => 0 [p] => 0 [post_parent] => – moffepoffe 2010-11-15 17:36:27

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濾波器分別增加了地方,並加入子句循環。我測試了一個類似的代碼與我自己的網站,但如果它不工作,請讓我知道。