Connecting The Dots

Cristi Burcă
Lisbon, Sep. 2012

a.k.a. scribu

Posts 2 Posts

* posts = posts, pages, attachments, CPTs

Defining Connection Types

function page_contributors_ctype() {
    p2p_register_connection_type( array(
        'name' => 'page_contributors',
        'from' => 'page',
        'to'   => 'user',
        'duplicate_connections' => true,
        'fields' => array(
            'contribution' => 'Contribution'
        )
    ) );
}

add_action( 'p2p_init', 'page_contributors_ctype' );

Pages a user has contributed to

function get_contributed_pages( $user_id ) {
    $query = new WP_Query( array(
        'connected_type' => 'page_contributors',
        'connected_items' => $user_id
    ) );

    return $query->posts;
}

Users that contributed to a page

function get_contributing_users( $page_id ) {
    return get_users( array(
        'connected_type' => 'page_contributors',
        'connected_items' => $page_id
    ) );
}

Storing the connections

First try: Custom Fields

wp_postmeta

Second try: Hidden Taxonomy

wp_terms

wp_term_relationships

Currently: Custom Tables

wp_p2p

wp_p2pmeta

The Plumbing

Core Classes

Admin Classes

The Unit Tests

References

Contact