2017-04-08 46 views
0

我正在使用Posts 2 Posts plugin。我有許多不同的帖子類型和許多不同的連接類型。WP_Query帖子之間的連接

我創建像這樣

function register_post_type_connections() { 
$connection_post_types = array('person', 'nonprofit', 'business', 'article', 'event', 'structure', 'government'); 
foreach($connection_post_types as $post_type){ 

    p2p_register_connection_type(array(
     'name' => $post_type .'_to_structure', 
     'from' => $post_type, // use $my_post_types if you didn't define $temp_array 
     'to' => 'structure', 
     'reciprocal' => false, 
     'duplicate_connections' => true, 
     'sortable' => true, 
    )); 
} 
// Foreach repeated for each $connection_post_types 
} 
add_action('p2p_init', 'register_post_type_connections'); 

的foreach循環重複7次總在此功能讓每一個可能的組合,所有這些關係。我已經測試了結果,並且它能正常工作

許多連接都已製作並附加到各個帖子頁。我想顯示所有這些連接的列表。

我得到的所有連接類型的這樣

function get_all_connection_types() { 
    $connection_types = array(); 
    $connection_post_types = array('person', 'nonprofit', 'business', 'article', 'event', 'structure', 'government'); 
    foreach($connection_post_types as $post_type){ 
     $connection_types[] = $post_type .'_to_person'; 
     $connection_types[] = $post_type .'_to_nonprofit'; 
     $connection_types[] = $post_type .'_to_business'; 
     $connection_types[] = $post_type .'_to_article'; 
     $connection_types[] = $post_type .'_to_event'; 
     $connection_types[] = $post_type .'_to_structure'; 
     $connection_types[] = $post_type .'_to_government'; 
    } 
    return $connection_types; 
} 

的完整列表,然後我跑我的循環

$post_types = array('person', 'nonprofit', 'business', 'article', 'event', 'structure', 'timeline', 'government'); 
    $connection_types = get_all_connection_types(); 
    $connected = new WP_Query(array(
     'connected_type' => $connection_types, 
     'post_type' => $post_types, 
     'connected_items' => 'any', 
     'connected_direction' => 'to', 
     'posts_per_page' => 20, 
     'orderby' => 'meta_value', 
     'connected_orderby' => 'date', 
     'connected_order' => 'desc' 

    ));   
    echo '<ul>'; 
    if ($connected->have_posts()) { 
     while ($connected->have_posts()) : 
     $connected->the_post(); 

下面是從數據庫

enter image description here

我的連接

我的循環只返回person_to_person連接。所以我改變connected_type到測試..

$connection_types = array('person_to_structure', 'person_to_person'); 

這給了我person_to_structure連接,但不是person_to_person。

爲什麼?

回答

1

您需要傳遞「connected_direction」作爲與「connected_type」具有相同長度的數組,以便將此代碼更改爲索引文件中的代碼。

$post_types = array('person', 'nonprofit', 'business', 'article', 'event', 'structure', 'timeline', 'government'); 
$connection_types = get_all_connection_types(); 
$direction_array = array(); 
for($i=0;$i<count($connection_types);$i++) { 
    $direction_array[$i] = 'from'; // if you want then you can send the from as well; 
} 
$connected = new WP_Query(array(
    'connected_type' => $connection_types, 
    'post_type' => $post_types, 
    'connected_items' => 'any', 
    'connected_direction' => $direction_array, 
    'posts_per_page' => 20, 
    'orderby' => 'meta_value', 
    'connected_orderby' => 'date', 
    'connected_order' => 'desc' 
)); 

這將工作。