Support

Account

Home Forums Front-end Issues Query custom post type depending on post object inside a repeater

Solving

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

  • Hi @cedric_charles

    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

  • Hi @cedric_charles

    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 ๐Ÿ™‚

  • This reply has been marked as private.
  • Hi @cedric_charles

    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 ๐Ÿ˜‰ !

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.