#57491 closed defect (bug) (invalid)
Php 8.2.0 - error if the Wp Query does not have any post
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | major | Version: | 6.1.1 |
Component: | Query | Keywords: | php8 needs-testing |
Focuses: | Cc: |
Description
Hello,
I use php version 8.2.0 and wordpress 6.1.1
I have custom query. Everything works well. But if the WP_Query does not have any post I see this php error message:
Warning: Undefined array key 0 in /data/web/virtuals/291601/virtual/www/wp-includes/class-wp-query.php on line 3663
Please, fix it in the next WordPress version.
Thank you,
David
Change History (11)
#2
in reply to:
↑ 1
@
8 months ago
Ok, I tested it with the default wordpress theme "Twenty Twenty-Two" and disabled all plugins. The problem is still the same.
So it is not because of my theme or any plugin. It is because WordPress is still not ready for php 8 and higher. I use php 8.2.0
#4
@
8 months ago
Here is my custom query wich I use, but I don't see problem here:
<?php $args = array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 10, 'ignore_sticky_posts' => 1, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'post_wish_count', 'tax_query' => array('relation' => 'AND', array('taxonomy' => 'typ', 'field' => 'slug', 'terms' => array('produkt')), array('taxonomy' => 'typ', 'field' => 'slug', 'terms' => 'vyprodano', 'operator' => 'NOT IN'))); $query_katalog = new WP_Query($args); if($query_katalog->have_posts()): while($query_katalog->have_posts()) : $query_katalog->the_post(); the_title(); endwhile; else:?> <p>Nothing found</p> <?php endif; wp_reset_postdata();?>
#7
@
5 months ago
Hi,
Based on the custom query I'm assuming you have more custom code or some plugins installed so I've switched to native taxonomies (category) and manually added the post_wish_count
. I've tested it on PHP 8.2.0 and WP 6.3 (develop version), Twenty Twenty-Three theme, no additional plugins and got no notices. So I guess it was fixed in new version?
<?php $args = [ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 10, 'ignore_sticky_posts' => 1, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'post_wish_count', 'tax_query' => [ 'relation' => 'AND', [ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => ['test1111'], ], [ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => 'test22222', 'operator' => 'NOT IN', ], ], ]; $query = new WP_Query($args); if ( $query->have_posts() ) { foreach ($query->get_posts() as $post) { echo "{$post->post_title} <br>"; } } else { echo "Nothing found"; }
#8
@
5 months ago
- Keywords php8 added
The warning comes from WP_Query::rewind_posts()
, called as part of ::have_posts()
.
If I'm reading the code correctly, it can be triggered if $this->post_count
is greater than zero but $this->posts
is empty, though it's not quite clear to me how to reproduce that scenario. The queries from comment:4 or comment:7 don't seem to trigger it in my testing.
#9
@
5 months ago
I couldn't replicate the notice either using TwentyTwentyOne with the code from comment:7.
Checking other tickets on trac related to rewind_posts
, I saw a similar report in #41446. It seems a notice is displayed when the global variable $posts
is directly overridden with another query which yield no results. But I don't know if it is the same issue here.
Thanks for the report @davidki! Can you reproduce the problem using one of the default "Twenty-" themes? This looks like it might be an issue in your theme or one of the plugins on the site. Cheers.