Home › Forums › Front-end Issues › Query custom post type depending on post object inside a repeater
Hi guys !
I have a website where I show books and books authors. Each book has a “book_authors” field (that retrieves objects), that links the book to one or multiple authors.
I have an array of authors (array of objects) and I would like to show in front all the books that have at least one of these authors in the “book_authors” field. I’ve tried a lot of things but it doesn’t work…
Here is the last version of my code :
($lawyers_ids is the array of author’s objects)
$models2 = new WP_Query(array(
'post_type' => 'book',
'paged' => $paged,
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'book_authors',
'value' => $lawyers_ids,
'compare' => 'IN'
),
Unfortunately, it doesn’t shows all the books it should… Why is that ?
Thank you in advance,
CΓ©dric
I suggest you use the relationship field for the “book_authors” field. After that, you can check this page to learn how to query it: https://www.advancedcustomfields.com/resources/querying-relationship-fields/.
I hope this helps π
Hello @acf-support π !
It’s exactly the type of field I use : the relationship one π ! It returns an array of objects.
In your example, they query only on 1 ID. I need to query if at least one author is the author of a book. So here is what I want :
$lawyers_ids contains an array of multiple objects of the “author” CPT. I would like to query the books that have at least one of these authors in their “books_authors” relationship field.
Thank you in advance,
CΓ©dric
Then you need to build the query for each lawyer ID. I believe it should be something like this:
// initialize the meta query
$meta_query = array('relation' => 'OR');
// Add the query for each lawyer ID
foreach( $lawyers_ids as $lawyers_id ) {
$meta_query[] array(
'key' => 'book_authors',
'value' => '"' . $lawyers_id . '"',
'compare' => 'LIKE'
);
}
// Get the books
$books = get_posts(array(
'post_type' => 'book',
'paged' => $paged,
'posts_per_page' => -1,
'meta_query' => $meta_query,
));
I hope this helps π
ACF saves the ID instead of the object in the database to minimalize the storage needed. This means that we need to compare against the ID when you query the database.
The options you’ve set is used when you use the get_field()
and the the_field()
function. It will allow ACF to convert the ID to the selected type automatically.
I hope this makes sense π
Hi @acf-support !
It totally makes sense π ! Good to know, Thank you very much ! Very powerful support π !
The topic ‘Query custom post type depending on post object inside a repeater’ is closed to new replies.
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.