Home Forums Backend Issues (wp-admin) PHP 8.0 Uncaught TypeError on relationship filter


PHP 8.0 Uncaught TypeError on relationship filter

  • Hi,

    We have a relationship filter with ACF Pro 6.0.3 on WP 6.0.3 which is producing the following error:

    PHP Fatal error:  Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, string given in C:\www\themandarin-vip\wp-content\plugins\advanced-custom-fields-pro\includes\fields\class-acf-field-relationship.php:261
    Stack trace:
    #0 \wp-content\plugins\advanced-custom-fields-pro\includes\fields\class-acf-field-relationship.php(92): acf_field_relationship->get_ajax_query(Array)
    #1 \wp-includes\class-wp-hook.php(307): acf_field_relationship->ajax_query('')
    #2 \wp-includes\class-wp-hook.php(331): WP_Hook->apply_filters('', Array)
    #3 \wp-includes\plugin.php(476): WP_Hook->do_action(Array)
    #4 \wp-admin\admin-ajax.php(187): do_action('wp_ajax_acf/fie...')
    #5 {main}
      thrown in \wp-content\plugins\advanced-custom-fields-pro\includes\fields\class-acf-field-relationship.php on line 261

    This is preventing admins from being able to use the field. We’ve disabled the filter for the time being.

    For your reference the filter code is:

    function mandyacf_homepage_posts( $args, $field, $post_id ) {
    	$rs_paged = 1;
    	if ( ! empty( $args['paged'] ) ) {
    		$rs_paged = $args['paged'];
    	$rs_s = '';
    	if ( ! empty( $args['s'] ) ) {
    		$rs_s = $args['s'];
    	$args = array(
    		'post_type'      => 'post',
    		'posts_per_page' => '20',
    		's'              => $rs_s,
    		'post_status'    => 'publish',
    		'orderby'        => 'ID',
    		'order'          => 'DESC',
    		'paged'          => $rs_paged,
    		'date_query'     => array(
    				'after' => gmdate( 'F jS, Y', strtotime( '-30 days', time() ) ),
    			'inclusive' => true,
    	return $args;
    add_filter( 'acf/fields/relationship/query/key=field_59add5e2bcfa4', 'mandyacf_homepage_posts', 10, 3 );

    I’ve found that if I comment out the code in advanced-custom-fields-pro\includes\fields\class-acf-field-relationship.php on line 261, then it works a treat.

    Any idea what’s going on here?

  • The problem is that ACF is expecting the post_type argument to be an array at this point in the code and you are setting it to a string in your filter.

    As a work-a-round use

    'post_type'      => array('post'),

    the filter you are using happens after ACF has formatted the arguments and before it formats the values into json.

    I suspect that this needs to be adjusted for PHP8 to see if the post_type argument is a string or not before line 261. But I can’t fix that. Contact the developers here.

  • Thanks Josh – much appreciated!
    There was a bit of a facepalm moment on this end, but very grateful for the feedback.

  • Actually, you are not wrong, the WP_Query argument will take an array or a string here, it is just that ACF is assuming an array and using that array when deciding what to return in the AJAX request.

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

You must be logged in to reply to this topic.