If you’re learning how WordPress templates work, there are two kinds of functions you’ll see a lot. What I like to call, the “get” and “the” template functions. If you haven’t noticed by now, there are functions in WordPress that seemingly do the same thing, so what is the difference? When do you use either one? Here are some examples:
|“Get” function||“The” function|
I learned PHP through WordPress first, before I went on to solidify my PHP knowledge outside of it. If you’re in the same boat that I was, even if you’re not a beginner in other languages like HTML and Css, here is how I learned these functions.
The “get” functions will fetch the data I want, but the data will not display on the front-end by default. If I drop one of these functions, say “get_the_title” inside of a WordPress loop, I will not see the title on the front-end. For those who don’t know what I mean by front-end, I mean you won’t see it on your website. Which is confusing, right? At first, I was like, “Hey, I’m in the loop, and it’s supposed to get the title, so why isn’t the darn title showing?”
It’s because “get” functions do just that, they get the data, but that’s it. It’s like telling your pet dog to fetch the stick behind the couch, and so your dog runs behind there. You expect your dog to come right back out with the stick, but he doesn’t.
The function is technically working because, your dog, has the stick you asked it/him to get. However if you want the actually see the data, or the stick, you have to use a PHP language construct called “echo”. Functions that return data, like the “get” functions, will display the data you’ve asked it for, if you add “echo” before them.
Here is an example below:
echo get_the_title( $post->ID );
Another difference with the “get” functions, is that you can pass a post ID to them, and therefore they are best used outside the loop. While you can use them inside the loop, it can be redundant if you pass a post ID, because these functions will already know the post ID due to the loop.
Which brings me to the “the” functions. These functions, unlike the “get” functions, will display the data on the front-end automatically. Meaning you will not have to use “echo” to see the data. Continuing with my dog example, this is where the dog not only fetches the stick, but he knows to come back and show you the stick.
For “the” functions, you cannot pass a post ID to them. Therefore they are best used inside the WordPress loop.
Of course there are always exceptions. These simplified rules are also something that applied when I was learning WordPress years ago, and since then, these functions have evolved.
There are cases where you can use the “the” function inside of a loop, but don’t have to use echo. This way the function can behave similar to it’s “get” counterpart.
An example of this is “the_title”. If you read the parameters that this function allows, the last parameter says you can set the “echo” to true or false. By default, this function will echo/display the title inside of the loop.
//parameters it accepts as of the date this article was published the_title( string $before = '', string $after = '', bool $echo = true ) //this is it's default, if you pass nothing, it will echo on it's own the_title( '', '', true ); //if you don't want it to display, you can now set echo to false so it can behave a little like it's "get" function get_the_title() the_title( '', '', false );
By passing the_title false, it will not show the title and instead fetch the data without displaying it. Why would you ever want to do that inside of a WordPress Loop? I’ve written about some cases where this can be useful, such as read more links in my recent article: The Screen Reader Text Class: 5 Real Life Applications.
There are also exceptions to “get” functions always allowing you to pass a post ID as a parameter. This is why it’s good to check the WordPress Codex/Code Reference for a sanity check if something in your template doesn’t appear to be doing what you want it to do, or you’re seeing errors. The exception here is “get_the_content”. If you look at the parameters for this function, you actually can’t pass a post ID and therefore, you can not use it outside the loop.
Here is what the parameters for get_the_content look like, and what you should use instead when you’re trying to get content outside the loop.
//the parameters, none that are the post ID get_the_content( string $more_link_text = null, bool $strip_teaser = false ) //one way you can get the content outside of a loop with a post ID $post_object = get_post( $post_id ); echo $post_object->post_content;
Above is just one alternative for when you want to pass the ID to get post content.
The last exception I’ll mention so we can wrap this quick tip up, are the functions regarding post dates. The two functions I’m referencing are get_the_date and the_date.
Now, the exception here isn’t that they don’t follow the simplified rules I mentioned earlier. Yes you can use the “get” version outside of the loop, and yes you can pass it a post ID. You can also use the “the_date” inside of the loop, and it does not accept a post ID as a parameter. So, what’s the problem, right?
The problem is, the_date, unlike the other “the” functions like for title, permalinks and so on, does not behave the way you expect. There is a “special note” on the codex that mentions this and years ago when I was using the function, it would make me scratch my head a bit. I’ll copy the special note from the codex below:
SPECIAL NOTE: When there are multiple posts on a page published under the SAME DAY, the_date() only displays the date for the first post (that is, the first instance of the_date()). To repeat the date for posts published under the same day, you should use the Template Tag the_time() or get_the_date() (since 3.0) with a date-specific format string.
<?php the_time( get_option( 'date_format' ) ); ?>to add the date set in the admin interface.
The exception is that if you are using the_date in a loop, and there are multiple posts, posts published on the same date will only show the date for the first post. So there I was, years ago, wondering why the date was only showing one time. There were no errors, and I was using the function how it was supposed to be used, or so I thought. Turns out it was the function, not me.
So in the case of retrieving the date, whether it’s inside or outside the loop, I’ve gotten into the habit of always using the “get” version. This way I can guarantee that I always have the date for every post, and I can choose whether or not to display it with echo.
Summarizing the difference between “get” and “the” template functions
Since this is a quick tip, let’s very quickly review what I’ve went over. I’m a visual person and I find tables can be helpful.
|“The” Functions||“Get” Functions|
|Usually best used inside of the WordPress loop.||Usually best used outside of the WordPress loop but can be used inside as well.|
|You cannot pass a post ID as a parameter.||You can pass a post ID as a parameter, in most cases. An exception is get_the_content.|
|Will display by default. There are exceptions where you can choose not to display. One exception is the_title if you set it’s last parameter to false.||Will not display by default. Just returns data instead unless you add “echo” to it so that it’s visible on your site.|
|the_date follows all the rules I mentioned, but it will not display the post dates on posts with the same dates more than once in a loop.||get_the_date follows all the rules I mentioned, but because of the “quirk” that it’s “the” counterpart function has, I like to always use the “get” version for post dates both in and outside the loop.|
That’s all folks! I hope this quick tip was quick enough to absorb on the go. Since I’m still working on this series and it’s format, if you have any feedback, I’d love to hear it. Until next time, happy WordPressing.
“Before my web dev days, I worked in an after school program doing office work and teaching kids arts & crafts. One day, the boss wanted to have a meeting that would end late at night. It wasn’t a mandatory meeting. This job was not only a two hour trek, two trains and a bus away, but it also passed through unsafe neighborhoods. Going home alone at night was dangerous. My boss, bless her and her privilege, could not understand how or why I needed to skip this meeting. Said boss also had a car that she took to and from work. I didn’t go. She docked me a day of pay as “punishment.” I’ve also turned down almost all after work/late night social events for the very same reason — it’s dangerous. That may apply to other people too.”
“It wasn’t a mandatory meeting”
“I didn’t go. She docked me a day of pay as “punishment.”
And that’s legal? If you worked for that money, they cannot legally take that money away for any reason.
You should have sued her ass.
I was barely an adult at the time. It was pretty much my first full time job after high school, so I had no inkling what employee rights were. It probably wasn’t legal of her to do that. Still, I wasn’t risking getting mugged for her.