Support

Account

Home Forums Bug Reports ACF returning ID instead of an array and count of repeater instead of an array

Solving

ACF returning ID instead of an array and count of repeater instead of an array

  • I get the posts through WP_Query and use posts_where filter to change the query parameters. After that, I try to get the fields that should return arrays. But it returns the file ID instead of array, and conut of repeater field instead of array.

    In normal query results, without using the posts_where hook – arrays are returned. But I need to change the query.

    I am using latest versions of WordPress, ACF PRO 5.3.2.2 and PHP 5.6.

    How do I get the arrays in this case?

  • Hi @almazka

    Could you please share the JSON export of your field group and the code you used so I can test it out on my installation?

    Thanks!

  • Here’s my situation. What do we have:
    Custom post type “items” which has the following field group “Item” with the one image type field. It should returns to array.
    Also the current user has a group of fields “User dictionaries”.
    My code is:

    $args = array(
    'posts_per_page' => '-1',
    'post_type' => 'items',
    );
    $myitems = new WP_Query( $args );
    if ( $myitems->have_posts() ) :
    	while ( $myitems->have_posts() ) :
    		$myitems->the_post();
    		$image = ( get_field( 'image' ) ) ? get_field( 'image' ) : false;
    		$dic = get_field( 'dictionaries', 'user_' . get_current_user_id() );
    		//code...
    	endwhile;
    endif;
    wp_reset_postdata();

    In this case get_field for $image returns an array as it should be and get_field for $dic returns an array of the repeater field dictionaries of current user as it should be.

    But my goal is to get on the archive page a certain kind of items.
    For this I use the hook “posts_where” for modified request.

    My code:

    global $wpdb;
    add_filter( 'posts_where' , 'my_where' );
    function my_where( $where ){
    	$where .= " AND wp_posts.post_title LIKE 'b%'";
    	return $where;
    }

    All items are returned correctly. But get_field for $image now returns the ID of image and get_field for $dic now returns the digital value of the number of fields of the repeater.

    Json file of the field groups is attached.

  • Hi @almazka

    I think it’s because your posts_where hook modified ACF’s fields (which is also a post apparently). I believe you need to check if the queried post is not acf-field like this:

    add_filter( 'posts_where' , 'my_where', 10, 2 );
    function my_where( $where, $query ){
        global $wpdb,$wp_query;
        if ($query->query_vars['post_type'] != 'acf-field') {
    	    $where .= " AND wp_posts.post_title LIKE 'b%'";
        }
    	return $where;
    }

    Hope this helps.

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

The topic ‘ACF returning ID instead of an array and count of repeater instead of an array’ is closed to new replies.