Why is the first query affecting the second query, even after wp_reset_query() and wp_reset_postdata(), but not on the second page?

I see a number of issues, aside from the use of query_posts ( this should raise immediate alarm bells ). I strongly recommend you don’t use the alt syntax, and that you minimise the unnecessary opening and closing PHP tags as it obscures your code. Here is a cleaned up version of your first query … Read more

How would I get 1 latest post from a query for 5 posts?

You can use $current_post property of WP_Query $query_args = array( “posts_per_page” => “5” ); $listedPosts = new WP_Query($query_args); // the loop if ( $listedPosts->have_posts() ) { while ( $listedPosts->have_posts() ) { $listedPosts->the_post(); if ( (int) $listedPosts->current_post === 0 ) { // loop content for hero post } else { // loop content for remaining posts … Read more

Why we use if with while loop?

You only need the if ( have_posts() ) : if, as the name of the function suggets, you need to do something different if you don’t have posts. This would be something like displaying a “No posts found.” message. But you’d only need that on templates that could show no posts, like archives and search. … Read more

loop through all posts memory error

The posts are still held in memory under WordPress’ cache mechanism (even though you replace $posts on every loop) – delete each one after operating on it: # do some echoing with the $post # wipe post from memory wp_cache_delete( $post->ID, ‘posts’ ); wp_cache_delete( $post->ID, ‘post_meta’ ); Pro tip: save some needless queries with no_found_rows … Read more

Is get_posts() more efficient than The Loop?

When WordPress include the category.php (just like any other template) the main query (i.e. the query triggered by the url) is already ran: it means that a query on database was already fired and this is the reason why in a template you can run the Loop without calling a query. If you call get_posts … Read more

Can’t get drafts with WP_Query using post_status parameter

Try passing it as an array. For example $args = array( ‘p’ => 1234, ‘post_type’ => ‘any’, ‘post_status’ => array(‘draft’) ); Or for all types $args = array( ‘p’ => 1234, ‘post_type’ => ‘any’, ‘post_status’ => array(‘publish’, ‘pending’, ‘draft’, ‘auto-draft’, ‘future’, ‘private’, ‘inherit’, ‘trash’) );

Group posts by year in loop

This little snippet could help you. $years = array(); if( have_posts() ) { while( have_posts() ) { the_post(); $year = get_the_date( ‘Y’ ); if ( ! isset( $years[ $year ] ) ) $years[ $year ] = array(); $years[ $year ][] = array( ‘title’ => get_the_title(), ‘permalink’ => get_the_permalink() ); } } This code gets all … Read more