• Resolved mikehermary

    (@mikehermary)


    I am experiencing the following error output when using the standard loop on page.php and single.php on a multisite-based WordPress installation. The loops are the same for both template types, with the loaded content template the only difference.

    I have added the $page = get_queried_object() to my page.php template file and dumped the output using var_dump( $page ). The WP_Post object is displaying the correct page object data for each page visited on the multisite. It looks like the data can be retrieve, but something in the loop is not working for some reason.

    I have included the var_dump( $page ) below for reference.

    object(WP_Post)#1531 (24) {
    ["ID"]=> int(157)
    ["post_author"]=> string(1) "8"
    ["post_date"]=> string(19) "2024-08-21 10:49:40"
    ["post_date_gmt"]=> string(19) "2024-08-21 16:49:40"
    ["post_content"]=> string(155) "This page will act as the primary landing page for various content that belongs in the contact section."
    ["post_title"]=> string(7) "Contact"
    ["post_excerpt"]=> string(0) ""
    ["post_status"]=> string(7) "publish"
    ["comment_status"]=> string(6) "closed"
    ["ping_status"]=> string(6) "closed"
    ["post_password"]=> string(0) ""
    ["post_name"]=> string(7) "contact"
    ["to_ping"]=> string(0) ""
    ["pinged"]=> string(0) ""
    ["post_modified"]=> string(19) "2024-10-15 12:36:20"
    ["post_modified_gmt"]=> string(19) "2024-10-15 18:36:20"
    ["post_content_filtered"]=> string(0) ""
    ["post_parent"]=> int(0)
    ["guid"]=> string(50) "https://beta.domain.com/child-site/?page_id=157"
    ["menu_order"]=> int(0)
    ["post_type"]=> string(4) "page"
    ["post_mime_type"]=> string(0) ""
    ["comment_count"]=> string(1) "0"
    ["filter"]=> string(3) "raw"
    }

    Error

    Warning: Undefined array key 0 in /var/www/domain.com/wp-includes/class-wp-query.php on line 3671 and Warning: Undefined array key 0 in /var/www/domain.com/wp-includes/class-wp-query.php on line 3766

    Affected Functions from class-wp-query.php

    public function next_post() {
    ++$this->current_post;
    /** @var WP_Post */
    $this->post = $this->posts[ $this->current_post ];
    return $this->post;

    }
    public function rewind_posts() {
    $this->current_post = -1;
    if ( $this->post_count > 0 ) {
    $this->post = $this->posts[0];
    }
    }

    Var dump

    if ( have_posts() ) :
    while ( have_posts() ) :
    var_dump( the_post() );

    get_template_part( 'template-parts/content', 'page' );

    // If comments are open or we have at least one comment, load up the comment template.
    if ( comments_open() || get_comments_number() ) :
    comments_template();
    endif;
    endwhile;
    endif;

    The returned output of var_dump( the_post() ); is NULL.

    Content Page template partial

    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">
    <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
    </header><!-- .entry-header -->
    <div class="entry-content">
    <?php the_content(); ?>
    </div><!-- .entry-content -->
    </article><!-- #post-<?php the_ID(); ?> -->

    There are two custom post types and taxonomies registered. They have been disabled for testing and do not have an effect on the errors.

    I have commented out various functions in my theme’s functions.php file for testing purposes. None of the testing has yielded a successful result. None of the functions utilize the the_content() function.

    I have changed to the twentytwentyfour theme for testing and the pages and posts do load correctly. After re-enabling my custom theme, I disabled all plugins individually including Advanced Custom Fields Pro with no success.

    I am at a loss on why this is occurring. I manage other multisites with none of these issues.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter mikehermary

    (@mikehermary)

    I have done some further testing and have included those results below.

    I have tested $query = new WP_Query( array( 'post_type' => 'page' ) ) and outputted the $query using print_r(). The correct output is displayed for all of the pages on the child site (multisite). It also works when I add a specific page ID to the query $query = new WP_Query( array( 'post_type' => 'page', 'page_id' => 128 ) );

    I have checked my functions.php file for any pre_get_posts, $query, or WP_Query code, but none exists. The $post is used for various admin section-only functions, so I do not see a conflict there.

    Moderator bcworkz

    (@bcworkz)

    The error is because $this->posts array does not have an index 0 element. For a single page this element should contain the requested page/post object. You apparently have code somewhere that’s corrupting this array. On your template, I suggest you declare global $wp_query;, then print_r( $wp_query->posts ); at various places in your template code to determine exactly where it gets corrupted. It’ll likely be a function call. You may need to temporarily do something similar within that function’s source code to determine exactly where the array gets corrupted. If this function is a WP core function, the corruption likely occurs at a filter or action hook. Once you know which hook, look for callbacks added to that hook in your code. One of them is the likely suspect.

    Thread Starter mikehermary

    (@mikehermary)

    @bcworkz Thanks for the reply and for pointing me in a helpful direction.

    I identified the issue as a get_posts() implementation in a template partial that was executed before the main loop and was not using setup_postdata( $post ) at the start of the foreach and a wp_reset_postdata() after the foreach had completed.

    After making this change, the main loop is now working as expected.

    Cheers,

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.