0

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.

Update Two - 10/16/24

I have also 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.

Update - 10/16/24

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/rm-template/?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.

    1 Answer 1

    0

    I have resolved this issue after reviewing a get_posts() function that was running in a template partial that was not using setup_postdata( $post ) at the start of the foreach and closing out with a wp_reset_postdata() after the foreach had completed.

    This get_posts() was corrupting the main loop array and causing it to fail on page.php and single.php.

    Fixed get_posts function

    // Simplified for ease of reading $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'numberposts' => 1, ); $posts = get_posts( $args ); <?php if ( $posts ) : ?> <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?> <!-- Post Content Output --> <?php endforeach; wp_reset_postdata(); ?> <?php endif; ?> 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.