• Resolved madanpraba

    (@madanpraba)


    After upgrade to wp 6.1, wp thrown a php warning “Trying to access array offset on value of type int in /var/web/site/public_html/wp-includes/class-wp-list-util.php on line 170”

    debug log pointed out the below code. Please anyone help me to identify my mistake.

    $tags_list = '';
    $position_list = '';
    
    $tags = get_the_tags();        
    if( !is_wp_error( $tags ) && !empty( $tags ) && isset($tags) ) {
          $tags_list = wp_list_pluck( $tags, 'slug' );
    }
    $positions = get_the_terms($post, 'position');
    if( !is_wp_error( $positions ) && !empty( $positions ) && isset($positions) ) {
         $position_list = wp_list_pluck( $positions, 'slug' );
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Dump out $tags or $positions. Something is in there that doesn’t belong. The return from get_the_terms() (or get_the_tags()) should only be one of three types. You’ve already eliminated WP_Error and false as possibilities. The only other possibility should be an array of WP_Term objects, yet somehow an integer has been introduced as an array element.

    In normal execution you should never encounter line 170, because it only applies to non-object array elements. We should only be encountering objects in the array, so something is not right with what’s returned by get_the_terms(). Because the return can be filtered, the root cause of the problem could be due to some other plugin or the active theme.

    Thread Starter madanpraba

    (@madanpraba)

    Thank you @bcworkz I will check with the default theme and update here.

    I had the same issues on several wp instance.

    For me the issue came from loop on custom WP_Query with $args('fields' => 'ids')

    WP cannot make a traditionnal while $the_query->have_posts() loop on a $args('fields' => 'ids') results

    So 2 ways to fix this notice :
    1) remove the $args('fields' => 'ids')
    or
    2) replace the while with a foreach
    `while ( $the_query->have_posts() ) : $the_query->the_post();
    //do stuff with get_the_ID();
    endwhile;`

    by

    foreach($the_query->posts as $post_id):
     // replace get_the_ID() by $post_id
    endforeach;

    Yes, This is the solution for this issue @mi-ca . Just figured it out now. ‘the_posts’ method in WP_Query class has this below function. when $args('fields' => 'ids') is passed in WP_Query as argument, this function argument $posts becomes postIds array i.e(array of integers) instead of object array where ‘wp_list_pluck’ function can’t pluck the post author ids from array of integers. So, unset( $args['fields'] ); when ‘field’ already exists.

    function update_post_author_caches( $posts ) {
    	$author_ids = wp_list_pluck( $posts, 'post_author' );
    	$author_ids = array_map( 'absint', $author_ids );
    	$author_ids = array_unique( array_filter( $author_ids ) );
    
    	cache_users( $author_ids );
    }
    • This reply was modified 2 years, 4 months ago by yahyahaled.
    • This reply was modified 2 years, 4 months ago by yahyahaled.
    • This reply was modified 2 years, 4 months ago by yahyahaled.
    • This reply was modified 2 years, 4 months ago by yahyahaled.
    Thread Starter madanpraba

    (@madanpraba)

    many thanks to @mi-ca and @yahyahaled for identifying this issue and resolving it.
    I hope the wp-core team will update this in the documentation.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘php warning – wp_list_pluck, wp-list-util’ is closed to new replies.