Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fl0ri opened this issue May 4, 2018 · 4 comments

Comments

@fl0ri
Copy link

fl0ri commented May 4, 2018

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).

@fl0ri fl0ri changed the title List all posts of a custom post type ordered by the number of connections they have to another cpt List all posts of a custom post type ordered by the count of connections they have to another cpt May 4, 2018
@bbtodd
Copy link

bbtodd commented Nov 22, 2019

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
Copy link
Author

fl0ri commented Nov 25, 2019

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

@bbtodd
Copy link

bbtodd commented Nov 25, 2019

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
Copy link
Author

fl0ri commented Nov 25, 2019

Wow. That’s neat. Thanks for posting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants