简体   繁体   中英

How to solve Undefined offset: 5 in /~/wp-includes/class-wp-query.php on line 3284 in wordpress?

Error

Undefined offset: 5 in /wp-includes/class-wp-query.php on line 3284

'posts_per_page' is set 5 to take 5 posts.

I don't understand why this 5 brings this error.


Current Code

function queryPopularPosts() {
    return new WP_Query(
        array(
            'meta_key' => 'post_views_count',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
            'posts_per_page' => 5
        )
    );
}

function displayPopularPosts() {
    $query = queryPopularPosts();
    if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                echo '<p>'.the_title().'</p>';
            }
    }
}


/wp-includes/class-wp-query.php on line 3284

public function next_post() {

        $this->current_post++;

        $this->post = $this->posts[ $this->current_post ]; //3284
        return $this->post;
    }

the_post() in /wp-includes/class-wp-query.php

public function the_post() {
        global $post;
        $this->in_the_loop = true;

        if ( -1 == $this->current_post ) { // Loop has just started.
            /**
             * Fires once the loop is started.
             *
             * @since 2.0.0
             *
             * @param WP_Query $this The WP_Query instance (passed by reference).
             */
            do_action_ref_array( 'loop_start', array( &$this ) );
        }

        $post = $this->next_post();
        $this->setup_postdata( $post );
    }

Array keys usually start at 0. Perhaps try $this->post = $this->posts[ $this->current_post - 1 ]; //3284 $this->post = $this->posts[ $this->current_post - 1 ]; //3284 instead?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM