Can’t reset the secondary query by wp_reset_postdata()

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

3 posts from each existing category on one page

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

Optimizing the blog loop

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

Do I need to reset the loop in this code?

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

tech