scribu / wp-posts-to-posts

Efficient many-to-many connections between posts, pages, custom post types, users.
http://wordpress.org/plugins/posts-to-posts/
969 stars 260 forks source link

List all posts of a custom post type ordered by the count of connections they have to another cpt #562

Open fl0ri opened 6 years ago

fl0ri commented 6 years ago

I have two cpts "partner" and "event". Now I want to list all "partners" according to the number of connections they have to "events". How do I query the connections to get the count? PS: Plus it would be awesome to display the actual count of connections alongside the cpt (in brackets).

bbtodd commented 4 years ago

Hello, I have the same issue. Have you discovered a way to query for posts based on the number of connections? This would be most useful for querying for user to posts connections to determine the most popular posts.

fl0ri commented 4 years ago

No, unfortunately not. I couldn't find a way to query the count of relations/connections.

bbtodd commented 4 years ago

OK, thanks for the reply. I figured their probably isn't a way to do this. So I built a workaround, I created a meta field in functions.php called 'p2p_post_counter' and then wrote a small plugin with functions that increment and decrement that field every time a connection is made.

I only need this data for user to post connections, so in my case I added code to the plugin Restful P2P which is primarily for that type of connection. Now when a connection is made or a connection is deleted it calls the function that updates the counter. https://thestizmedia.com/create-posts-2-posts-connections-wordpress-rest-api/

I could elaborate more if anyone is interested but the concept is pretty simple. The only drawback is it's not retroactive so to speak, so prior connections obviously won't be counted.

fl0ri commented 4 years ago

Wow. That’s neat. Thanks for posting.