Support

Account

Home Forums Add-ons Repeater Field Query by an array of values from an ACF repeater Post Object sub field

Solving

Query by an array of values from an ACF repeater Post Object sub field

  • I am building a record label website in WordPress which has an artists and a releases post type.

    The first part of my issue is as follows: I have a repeater field in releases called release_artist, containing a post object sub field called artist. I need to extract an array of post IDs from that sub field (sometimes a release has more than one artist associated).

    For example:
    Row 1: Artist = XXX (post ID 1)
    Row 2: Artist = YYY (post ID 2)

    The result I want to extract as a variable would in this case be the array 1,2.

    This is my attempt so far at extracting that array as a variable:

    
    if(have_rows('release_artist')): 
        while(have_rows('release_artist')): the_row();
            $select = get_sub_field_object('artist');
            $value = get_sub_field('artist');
            $artists = $select['value'];
        endwhile; 
        $artists_id = $artists->ID; // this should be an ID if only 1 repeater row, or array of IDs if more than 1
    else: endif;
    

    It’s returning only the ID of the sub field from the last repeater row.

    The second part of my issue is this: I then need to use this array in a query to find other releases posts that have one or more of the artist(s) associated. So far I have the following, which is also currently wrong:

    
    $artist_release_args = array(
        'post_type'     => 'releases',
        'meta_query'    => array(
            array(
                'key'       => 'release_artist_artist',
                'value'     => $artist_id, // this should give only results with one of the post IDs present in the array
                'compare'   => '='
            )
        ),
        'showposts'     => 6,
        'order'         => 'DESC',
        'post__not_in'  => array($post->ID),
    );
    
    $artist_release_query = new WP_Query($artist_release_args);
    

    I’m pretty sure that meta_query is wrong, but I’m really not sure how to fix it.

    In short:
    Q1: How do I extract the desired array of post IDs from the repeater field?
    Q2: How can I correctly compare this array against post IDs in the query?

    I’m running WordPress 6.4.3 and ACF Pro 6.2.7 in a local environment.

  • Hi there,

    About your first issue.
    When you’re using $artists = $select['value']; in your loop : you reset $artists variable every time a loop is completed.

    That’s why you store only the last value of your repeater field.

    I assume you’re working on the same file template.
    First, create an array of post IDs contained in your repeater field.
    Then, use this array to build your custom WP_Query.

    //Let's create an empty array
    $artists = array();
    
    //Loop over your repeater field
    if(have_rows('release_artist')):
      while(have_rows('release_artist')): the_row();
        $related_artists = get_sub_field('artist');
        //Working on post object
        if( $related_artists && is_object($related_artists) && !is_wp_error($related_artists) ) {
          $artists[] = $related_artists->ID;
        }
        //Working on an array
        if( $related_artists && is_array($related_artists) ) {
          foreach($related_artists as $post_object) {
            if( is_object($post_object) && !is_wp_error($post_object) ) {
              $artists[] = $post_object->ID;
            }
          }
        }
      endwhile;
      //You should have an array containing all post IDs from release_artists repeater
      //error_log(print_r($artists, true));
    endif;
    
    //Probably better to check if everything is valid in $artists array from there
    
    //Then you could use your array of post ID to build your custom Query
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $artist_release_args = array(
    		'post_type' 	 	 => 'releases',
    		'post_status' 	 => 'publish',
        'post__in'       => $artists,
    		'orderby' 		 	 => 'modified',
    		'order' 		 		 => 'DESC',
    		'posts_per_page' => 6,
    		'paged' 		 		 => $paged
    );
    $artist_release_query = new WP_Query($artist_release_args);

    Don’t forget to check if your query has posts when you loop over it.
    Add content-archive/content-none accordingly (or any template part needed).

    Note that is_object() and !is_wp_error() checks are probably not strictly necessaries.
    ACF normally returns an empty value if no post is selected in your sub field.

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

You must be logged in to reply to this topic.