Posts 2 Posts: Version 0.5

This release is focused on enhancing the API, making it easier to leverage the p2pmeta table.

First of all, a new variable for WP_Query is now available: connected_meta. It allows you to restrict connections based on what meta data they have. Here’s an example:

$my_query = new WP_Query( array(
    'post_type' => 'book',
    'connected_to' => 'any',
    'connected_meta' => array(
        'connection_date' => 'long ago',
    )
) );

This will retrieve any book that has a connection to any other post. Also, the connections have to have a custom field with the key connection_date and the value long ago.

And, if that’s not enough, you can use the advanced meta query syntax:

$my_query = new WP_Query( array(
    'post_type' => 'book',
    'connected_to' => 'any'
    'connected_meta' => array(
        array(
            'key' => 'connection_date',
            'value' => array( 'long ago', 'yesterday' ),
            'compare' => 'IN'
        )
    )
) );

Also, after you do the query, you can access the rest of the connection meta data via the p2p_id property, assigned to each found post:

while ( $my_query->have_posts() ) : $my_query->the_post();

    $connection_type = p2p_get_meta( $post->p2p_id, 'connection_type', true );

endwhile;

comments powered by Disqus