2017-04-08 3 views
0

Ich verwende die Posts 2 Posts plugin. Ich habe viele verschiedene Post-Typen und viele verschiedene Verbindungstypen.WP_Query Verbindungen zwischen den Posts

schaffe ich alle diese Beziehungen wie so

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

Die foreach-Schleifen wiederholt wird insgesamt 7-mal in dieser Funktion alle möglichen Kombinationen zu erhalten. Ich habe das Ergebnis getestet und es funktioniert einwandfrei.

Viele Verbindungen werden hergestellt und an einzelnen Post-Pags befestigt. Ich möchte eine Liste all dieser Verbindungen zeigen.

ich eine komplette Liste aller Verbindungstypen wie dieses

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

Dann laufe ich meine Schleife

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

Hier meine Verbindungen aus der Datenbank sind

enter image description here

Meine Schleifen gibt nur person_to_person Verbindungen zurück. So testete ich durch connected_type Wechsel zu ..

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

Das gibt mir die person_to_structure Verbindungen aber nicht die person_to_person.

Warum?

Antwort

1

Sie müssen übergeben "connected_direction" als Array mit der genauen Länge von dem "connected_type" hat für diese Änderung diesen Code auf Ihre Indexdatei.

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

Dies wird funktionieren.

Verwandte Themen