Home › Forums › General Issues › How to query books (CPT) by author (relationship)
Dear experts
Im not very good at PHP, but still have managed to setup a small wordpress showing books and their authors.
So I have two custom post types: Books and authors.
On the Book pages, I would like to query all books that have the author(s) of the current book have written. Most of the times, a book will only have one author. I have on each book-page a relationship field called “authors”. I just cant wrap my head around how to query all books that have same author as the current book.
I would like it to show up like this:
Books by [Author1]
– Book Title 1
– Book Title 2
Books by [Author2]
– Book Title 1
– Book Title 2
And is it possible to hide the current book in the query?
It is difficult for me to google this as I dont really know what my problem is called 🙂 Any help would be highly appreciated.

There is a tutorial on doing this type of query here https://www.advancedcustomfields.com/resources/querying-relationship-fields/
This can be made easier by creating a bidirectional relationship field, if it’s not too late to do so. Add this would mean updating every post with a relationship after the code is added.
https://www.advancedcustomfields.com/resources/bidirectional-relationships/
Hi John,
Thanks for your answer. It is not too late, so I will try to create a bidirectional field and see if I can make it work. Thanks again 🙂
Hi John,
I cant thank you enough for your time it reply my question and for writing this plugin. (I can see that you are the plugin author).
It works just as it should. Thank you again
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.