WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 6 years ago

#28383 closed enhancement (maybelater)

Adding have_the_post() as a shortcut to have_posts() and the_post() loops

Reported by: sc0ttkclark Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Query Keywords: has-patch close
Focuses: template Cc:

Description

In most examples and code usage, you'll see something like this:

<?php while ( have_posts() ) : the_post(); ?>
...templating here...
<?php endwhile; ?>

What I'm recommending is a shortcode to make this easier, because how many use cases are there really for using have_posts() without the_post()?

<?php while ( have_the_post() ) : ?>
...templating here...
<?php endwhile; ?>

The equivalent would be added to $wp_query->have_the_post() as well.

Just let me know if there's interest in this and I'll whip up the quick patch that would essentially do this:

/**
 * Sets up the next post if the WordPress query has results to loop over.
 *
 * @see WP_Query::have_the_post()
 * @since 4.0
 * @uses $wp_query
 *
 * @return bool
 */
function have_the_post() {

	global $wp_query;

	return $wp_query->have_the_post();

}

And in WP_Query:

/**
 * Sets up the next post if the WordPress query has results to loop over.
 *
 * @since 4.0
 * @access public
 * @uses WP_Query::have_posts()
 * @uses WP_Query::the_post()

 * @return bool
 */
function have_the_post() {

	if ( !$this->have_posts() ) {
		return false;
	}

	$this->the_post();

	return true;

}

Change History (12)

#1 @sc0ttkclark
7 years ago

Sorry, I said 'shortcode' above, but it was autocorrected somehow from 'shortcut', or at least that's what I'm going to claim :)

#2 follow-up: @SergeyBiryukov
7 years ago

  • Focuses template added

#3 in reply to: ↑ description @sc0ttkclark
7 years ago

  • Focuses template removed

Replying to sc0ttkclark:

because how many use cases are there really for using have_posts() without the_post()?

I wanted to clarify this point further, not saying that this is designed to replace all usage of have_posts(), but all usage of have_posts() that also uses the_post() right after have_posts(), specifically in while() loops.

There are literally no uses of while( have_posts() ) that I can find without a call to the_post() directly after it. Not in core, not in most of the plugins I've seen use it, nor cases of custom WP_Query objects calling via while( $query->have_posts() ).

#4 in reply to: ↑ 2 @sc0ttkclark
7 years ago

  • Focuses template added

Replying to SergeyBiryukov

Sorry, I was editing at the same time! Adding it back

#5 @jtsternberg
7 years ago

Love this! Let's make it happen. I guarantee it'll cut down on amateur mistakes which is a good thing considering the loop & template tags are intended to be new-developer friendly.

#6 @bradparbs
7 years ago

The Loop is probably the most confusing thing for new developers, so I'm all for anything that can make understanding it a little easier. I think this is a great patch!

#7 @gr0b1
7 years ago

Sounds great. +1
Will make a lot more sense.

#8 @SergeyBiryukov
7 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 4.0

#9 @kovshenin
7 years ago

How many use cases are there really for using have_posts() without the_post()?

In a while loop — none, because that would result in an infinite loop. In an if statement though, that's perfectly valid, because most often you don't want to advance the posts cursor until you actually hit the loop.

I have mixed feelings about this, and although I do agree that "the loop" in its current form is a bit awkward, I don't see how have_the_post() makes it any easier, especially once you start working with secondary loops, where you need wp_reset_postdata(). It think it's already a bit too implicit with the_post() and moving that inside have_the_post() makes it even more implicit.

This ticket was mentioned in IRC in #wordpress-dev by helen. View the logs.


7 years ago

#11 @helen
7 years ago

  • Keywords close added
  • Milestone changed from 4.0 to Awaiting Review

I am just not feeling this. No clear articulation on why, though I've tried to put something together.

#12 @wonderboymusic
6 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to maybelater
  • Status changed from new to closed

I am not into this - seems, to me, like too much reflection

Note: See TracTickets for help on using tickets.