Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress, why return $this->get_posts(); returns values?

Tags:

php

wordpress

I was trying to find out how wordpress process each request and returns the result. I found wp() function calls $wp->main() which in turn calls $this->query_posts(); and which calls $wp_the_query->query($this->query_vars) function in query.php file. The query() function calls return $this->get_posts(); and return the result.

My question is didn't see any any variables receiving this returned value so why this function has return, even though the wordpress works if I remove the return from the code. so what is the purpose of this return, (I guess this code saves the contents (posts) to $this->posts variable). btw I am using wp 3.6

like image 422
Joyal Avatar asked Jul 21 '26 09:07

Joyal


2 Answers

I believe this answer may provide what you're looking for:

https://wordpress.stackexchange.com/a/1755

Specifically this image (which I did not create myself):

WP Query

like image 122
nate Avatar answered Jul 24 '26 01:07

nate


The use of return is related to php (also used in other languages) not only WordPress. When execution reaches a return statement, the function stops and returns that value without processing any more of the function. A return with no value returns null and If there is no return keyword at the end of a function then, in this case too, a null value get returned.

Using a return statement at the end of a method/function is a good coding practice, it just returns the execution control back to the point, from where it started and it also Prevents Code Injection in PHP include files. Also, check this.

like image 22
The Alpha Avatar answered Jul 24 '26 01:07

The Alpha