WP_Query Connections Between Posts

enter image description here

I’m using the Posts 2 Posts plugin. I have many different post types and many different connection types.

I create all those relationships like 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' );

The foreach loops is repeated 7 total times in this function to get every possible combination. I’ve tested the result and it works correctly

Many connections are made and attached to individual post pags. I want to show a list of all these connections made.

I get a complete list of all the connection types like this

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

Then I run my loop

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

Here are my connections from the database

My loops is only returning person_to_person connections. So I tested by changing connected_type to..

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

This gives me the person_to_structure connections but not the person_to_person.

Why?

Read more here: WP_Query Connections Between Posts

Leave a Reply

Your email address will not be published. Required fields are marked *