• Resolved dukearmi

    (@dukearmi)


    Hi,

    I’ve been seeing the following error in my error log for months now, but it wasn’t a daily occurence. However, it started happening a few times daily in the last 4 days.

    PHP Warning: foreach() argument must be of type array|object, null given in …/content/themes/generatepress/inc/structure/post-meta.php on line 338

    This line is the first in this code:

    	foreach ( $items as $item ) {
    		$default_display = true;
    
    		if ( 'comments-link' === $item && is_singular() ) {
    			$default_display = false;
    		}

    Just before it is this code (just part of it):

    /**
     * Remove post meta items from display if their individual filters are set.
     *
     * @since 3.0.0
     * @param array $items The post meta items.
     */
    function generate_disable_post_meta_items( $items )

    I’m also using filters to display the updated date and link author name to a custom page (not author archive).

    I see no errors on the frontend.

    My question: Is this a theme coding issue or is it happening because of my filters maybe? Is there a way to fix it?

    Thank you!

Viewing 10 replies - 1 through 10 (of 10 total)
  • David

    (@diggeddy)

    Hi there,

    does the error provide more details such as a stack trace?
    which would normally show what functions were being called up to the point of the error occurring.

    Thread Starter dukearmi

    (@dukearmi)

    Hi David,

    It doesn’t provide any other information. The only stack trace I found in the error log was from September last year.

    Thread Starter dukearmi

    (@dukearmi)

    Hi David,

    The error provides no additional details. The only stack trace I found was from September last year.

    fernandoazarcon2

    (@fernandoazarcon2)

    Hi @dukearmi,

    I see. It would be hard to determine the cause of the error with no additional details.

    How are you adding your custom functions? Have you tried removing them temporarily?

    Can you also check if your post-meta.php is the same as this?: https://github.com/tomusborne/generatepress/blob/master/inc/structure/post-meta.php

    Thread Starter dukearmi

    (@dukearmi)

    Hi Fernando,

    I’m adding the custom fucntions with PHP code using the Code Snippets plugin. I haven’t tried removing it because it would mess with the post meta on the frontend. Here’s the code if it helps:

    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
        $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on %2$s</time>';
    
        $updated_time = get_the_modified_time( 'U' );
        $published_time = get_the_time( 'U' );
    
        if ( $updated_time > $published_time ) {
            $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Updated on %4$s</time>';
        }
    
        $time_string = sprintf( $time_string,
            esc_attr( get_the_date( 'c' ) ),
            esc_html( get_the_date() ),
            esc_attr( get_the_modified_date( 'c' ) ),
            esc_html( get_the_modified_date() )
        );
    
        return sprintf( '<span class="posted-on">%s</span> ',
            $time_string
        );
    }, 10, 2 );
    
    add_filter( 'generate_post_author_output', function() {
        return sprintf( ' <span class="byline">%1$s</span>',
            sprintf( '<span class="author vcard" itemtype="https://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<a href="%1$s" title="%2$s" rel="author"><span class="author-name" itemprop="name">%3$s</span></a></span>',
                esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
                esc_attr( sprintf( __( '', 'generatepress' ), get_the_author() ) ),
                esc_html( get_the_author() ),
                get_avatar( get_the_author_meta( 'ID' ) )
            )
        );
    } );
    
    add_filter( 'author_link', function( $link, $author_id ) {
        if ( 1 === $author_id ) {
            return 'https://www.example.com/author';
        }
        return $link;
    }, 10, 2 );
    
    add_filter( 'generate_header_entry_meta_items', function() {
        return array(
    		'author',
    		'date',
    		
        );
    } );
    
    /*
    add_action( 'generate_after_header', function() 
    		   {
    			   if ( is_single() ) {
    if (function_exists('rank_math_the_breadcrumbs')) 
    {
    rank_math_the_breadcrumbs(); 
    }
    		   }} );
    */
    add_filter( 'generate_show_title', '__return_false' );
    add_action( 'generate_after_header', function() {
    	if ( ! is_front_page() && ! is_archive() ) {
        the_title( '<h1 class="entry-title" itemprop="headline">', '</h1>' );
    }} );
    
    add_filter( 'generate_footer_entry_meta_items', '__return_null' );

    The last line of code has “return_null” in it so it might have something to do with the error.

    I used a text compare tool and it showed that my post-meta.php is the same as the one in the link your povided (no changes).

    David

    (@diggeddy)

    Try removing:

    add_filter( 'generate_footer_entry_meta_items', '__return_null' );

    And use this code instead to remove the footer meta:

    add_action( 'wp', function() {
      remove_action( 'generate_after_entry_content', 'generate_footer_meta' );
    } );
    Thread Starter dukearmi

    (@dukearmi)

    Hi David,

    Thank you very much! I’ll follow your advice and report if it resolves the issue in the next few days.

    David

    (@diggeddy)

    You’re welcome.

    Thread Starter dukearmi

    (@dukearmi)

    @diggeddy I’m happy to report that I haven’t seen the error for the past 3 days! Thank you so much for your help ??

    David

    (@diggeddy)

    Glad to hear that!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘PHP Warning: foreach() argument must be of type array|object, null given’ is closed to new replies.