why is WordPress printing my array instead of just constructing it?

Table of Contents

Intro

I am trying to apply a custom view to all the results of a visit to a taxonomy page, which involves wrapping the whole lot of results in some DOM boilerplate and then invoking the function to display each result as an element within that boilerplate. My strategy of choice was to step through each result, populated dynamically depending which taxonomy term has led them here, and fill a bucket with those results, then display each one appropriately.

In other words, I want the WordPress loop to tell me WHICH things to display, then my own iteration to decide HOW to display them. This seemed like a simple strategy, but loops like the following appear to be always displaying my bucket entire bucket, although I am nowhere telling it to actually print that bucket. Narrowing it down to a minimal example I have the following, which still is printing out $bucket. What is going on?

$bucket = array();
while ( have_posts() ) {
    the_post();
    $bucket[]=the_meta(); // this is all printed to the screen. Why?
}

Answer

From Ruvee@stackoverflow1:

Wordpress has this concept of thename of function and getthename of function. For example, thetitle and getthetitle. thetitle will echo out the title into the page but getthetitle will return the title instead.

thename of function will echo out the result. getthename of function, on the other hand, will return the result.

Read more on that.

Using getpostmeta did indeed do what we needed, although I’m still flumoxed as to why themeta is triggering a print that it doesn’t elsewhere in the code, and isn’t mentioned in the documentation. Perhaps it’s a feature of the theme we’re using.

Footnotes

1 Question posted here: https://stackoverflow.com/questions/69050961/why-is-wordpress-printing-my-array-instead-of-just-constructing-it

Tory Anderson avatar
Tory Anderson
Full-time Web App Engineer, Digital Humanist, Researcher, Computer Psychologist