Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Allow one-to-many, many-to-one connections #28

cyberhobo opened this Issue · 10 comments

3 participants


Given the setup in the Basic Usage wiki page, I'm wondering if I can write a query for pages that have no connected posts?

My guess is that this would require at least a new query variable to achieve.


Indeed. Perhaps 'not_connected' => 'any'.

May I ask what the use-case is?


It's kind of a convoluted use case from a client, and I admit this may be too rare to bother with. Simplified a bit: users can a write a post of type B about another post of type A. I'm trying to give them a list of type A posts they haven't already written about.


Actually, I've heard at least one other user want something like this:

He ended up using an additional query + 'post__not_in', which works, but might stop working if you have millions of posts.


How about if p2p_register_connection_type() could guarantee this:

$my_connection = p2p_register_connection_type( array(
  'from' => 'post',
  'to' => 'page',
  'cardinality' => 'one-to-many',
) );

And then, $my_connection->get_connectable( 'page' ) would return only posts that aren't already connected to any page.

The default cardinality would be 'many-to-many'.


I'll try something like that, thanks. I should be able to limit excluded posts to an author to keep the list manageable. BTW that link should be

@cyberhobo cyberhobo closed this

Actually, I want to implement this.

I had openend #16 earlier, but now I have a clearer picture of what's needed.

@scribu scribu reopened this

Missed your last comment - that feature seems like it would fit my situation (and be very cool :).


You know, really, modeling ActiveRecord associations would be incredible: belongs_to, has_many, has_one, etc. Dream the impossible dream, but...

@scribu scribu closed this in 36047b4

@doolin: ActiveRecord associations are interesting, but they're way out of scope for this issue.


Too lazy to create a new issue, so here goes:

ActiveRecord associations create foreign keys based on cardinality; P2P always uses the wp_p2p table.

Also, p2p_register_connection_type() tries to be impartial, whereas AR associations are focused on one end of the connection.

So, I don't think there's much benefit in replacing the 'cardinality' arg with 'has_one' or 'has_many'.

If you want ActiveRecord in PHP, see

@dbernar1 dbernar1 referenced this issue from a commit in dbernar1/wp-posts-to-posts
@scribu first pass at 'cardinality' arg. see #28 9320e02
@dbernar1 dbernar1 referenced this issue from a commit in dbernar1/wp-posts-to-posts
@scribu check other cardinality in get_connectable(). closes #28 677974e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.