0

I'm working with a custom theme that someone else created, and it's becoming obvious the author(s) were not familiar with WordPress. I'm doing cleanup and I'm just finding all sorts of bad things. They use ACF heavily and as such, have decided that using "the loop" is not necessary. Page templates essentially only have the following:

  • comment block with template name
  • get_header()
  • a series of ACF get_field() calls
  • get_footer()

This works, but I've never seen page templates created like this.

My question is not just what best practice is (I feel like this should all be wrapped in the standard have_posts/the_post loop even if it's only a single post/page template), but how does this work without even a call to "the_post()" AND is there likely some danger that some future version of WP will break this method of getting post data.

1 Answer 1

2

but how does this work without even a call to "the_post()"

Template tags like the_title(), the_content() etc., as well as the ACF get_field() functions, all use the global $post variable to determine which post's title, content etc. to display (unless a specific ID is specified).

When a WordPress page is loaded, WordPress performs the "main" query, which is what determines which posts will be used in The Loop. It also parses the URL and determines what to query, sends headers, and handles 404s.

The last thing it does though, is assign some global variables. One of those is the global $post variable. This is automatically set to the first post in the query's results. It also sets a couple of other global variables.

Because the global $post variable is set at this early stage, functions like the ones I mentioned earlier will generally work as expected when on a single page or post, even without The Loop.

The reason this is not sufficient though, is that the the_post() function that is run during the loop does several things that the main query has not already done. This includes:

  • Firing the loop_start action.
  • Iterating to the next post in the loop, when there are more than one.
  • Setting the in_the_loop() to true.
  • Setting several global variables not set in the main query, including: $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, and $numpages.

So if you don't run while ( have_posts() ) : the_post() then none of the above will happen. In many circumstances, including — apparently — yours, this might not seem to cause any trouble. But any plugins or other functionality that relies on any of the above will probably not work correctly.

is there likely some danger that some future version of WP will break this method of getting post data.

It's arguably already broken. I would be surprised if there was a change in the short to medium term that would break anything that's currently working for you (not setting the global $post variable during the main query would likely break many sites), but the only way to be safe is to do things the correct way.

1
  • This is wildly off-topic, but I really appreciate this answer, and it's very clear and well-written. I know there are a ton of WP books, are you (or anyone passing through) aware of a good book that talks about the general architecture of WP? For example, and expanded version of this answer as a chapter that sort of details the loading of a single page/post would be a great reference to have.
    – sporker
    Commented Sep 16, 2019 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.