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'.
'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.
$my_connection->get_connectable( '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
Actually, I want to implement this.
I had openend #16 earlier, but now I have a clearer picture of what's needed.
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...
first pass at 'cardinality' arg. see #28
check other cardinality in get_connectable(). closes #28
@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 http://www.phpactiverecord.org/