What’s causing an infinite loop?
What’s causing an infinite loop?
What’s causing an infinite loop?
wp_reset_query is meant to be used with a loop, because it resets the global $wp_query object. get_posts, on the other hand, just returns an array to be iterated with ie. foreach. setup_postdata does not populate the $wp_query object: https://codex.wordpress.org/Function_Reference/setup_postdata. For a custom query, you would either use the WP_Query class, or you could reset your … Read more
After reading the documentation for multiple loops in one page here, I only had one WP_Query for both the loops. I stored the ID’s of all the posts from the desired category and check for them in the second loop and continue over them. Here’s the final code – first loop <?php $args = array( … Read more
wp_reset_postdata not working
I found a problem. I had to change this: $args = array( ‘cat’ => $category->name, ‘posts_per_page’ => 3,); for this: $args = array( ‘category_name’ => $category->name, ‘posts_per_page’ => 3,); In first code I was passing category name which is string to argument expecting ID of category. Here is Codex reference where I found solution: category_name … Read more
Displaying requested data from a the database in wordpress
Use wp_reset_query() to restore the original query. This also calls wp_reset_postdata, so you can just replace that.
First of all: do not use query_posts use get_posts or new WP_Query instead. If you want to show only most viewed posts, you’ll need to add action ( for ex. wp ) check if your on single post page and update that post meta views ( +1 ). Then you could do something like these … Read more
You are missing the point of restoring the context to original state. Yes, your loops do work without reset. But anything that tries to access queried post after them will hit leftovers of your custom queries in global variables, rather than post from main query. In a literal sense the reset after your first query … Read more
wp_reset_postdata() wp_reset_postdata() will restores the global $post variable to the current post in the main query. This is useful when using WP_Query to customize loops or create multiple loops on the same page. This Answer will solve all your queries about wp_reset_postdata() & WP_Query & get_posts