My personal understanding about wordpress's $ post global variables and the loop of the main loop.

Source: Internet
Author: User
Tags php template

 

During the past two days, when I was creating my own website blog, I encountered some problems with the $ post global variable when I was modifying the topic. Now make a point record.

========================================================== ========================================================== ===

Http://codex.wordpress.org/Global_Variables

This is the official global variable list of wp. All wp global variables are involved, but most of them are not explained in detail.

========================================================== ========================================================== ===

Some member attributes of $ post, refer to this URL: http://codex.wordpress.org/Function_Reference/get_post

The value returned by the get_post () function can be object/array.

========================================================== ========================================================== ===

Online description of the loop and global variables in the main loop.

Http://fairyfish.net/2007/07/07/global-variables-and-the-wordpress-loop/

It is mentioned that:

InThe_post ()After a function is called, you can use many template functions and global variables.

And:

Global VariablesPostYou can use the following code to call it:

global $post;echo $post->post_title;

PassPostYou can also obtain the global variables: ID, post_author, post_date, post_excerpt, comment_count, and others.

According to the author, the $ post variable can only be called in the main loop, that is, it is not assigned a value before the_post () function is called.

However, reality is often contrary to human ideals. We often see some WP template Tutorials that use $ post-> XXX to get a lot of content, including title and content.

These calls are often placed outside the main loop. The most typical example is how to set the WordPress <meta> tag:

Http://www.chinaz.com/web/2011/0605/186189.shtml

$ Post-> post_content, $ post-> post_excerpt and so on are called, and the code is put in the header. in the PHP template file, we all know that the_post () and other functions are after the get_header () function.

========================================================== ========================================================== ===

In this case, is there a conflict?

Here, a strong voice in my heart is calling to solve this conflict.

========================================================== ========================================================== ===

I started to search for the source code of wp ......

First, I saw the code in the class-wp.php file:

Function register_globals (){
Global $ wp_query;
// Extract updated query vars back into global namespace.
Foreach (array) $ wp_query-> query_vars as $ key => $ value ){
$ GLOBALS [$ key] = $ value;
}

$ GLOBALS ['query _ string'] = $ this-> query_string;
$ GLOBALS ['posts'] = & $ wp_query-> posts;
$ GLOBALS ['post'] = (isset ($ wp_query-> post ))? $ Wp_query-> post: null;
$ GLOBALS ['request'] = $ wp_query-> request;

If (is_single () | is_page ()){
$ GLOBALS ['more'] = 1;
$ GLOBALS ['singles'] = 1;
}
}

Therefore, $ post and some similar global variables belong to the wp_query object.

In the query. php file, I found the definition of the wp_query object:

Function & query_posts ($ query ){
Unset ($ GLOBALS ['wp _ query']);
$ GLOBALS ['wp _ query'] = & new WP_Query ();
Return $ GLOBALS ['wp _ query']-> query ($ query );
}

Next, let's take a look at some class members and functions of WP_Query:

Class WP_Query {
//...
Var $ posts;
Var $ post;
//...
Function the_post (){
Global $ post;
$ This-> in_the_loop = true;

If ($ this-> current_post =-1) // loop has just started
Do_action_ref_array ('loop _ start', array (& $ this ));

$ Post = $ this-> next_post ();
Setup_postdata ($ post );
}
//...
Function next_post (){

$ This-> current_post ++;

$ This-> post = $ this-> posts [$ this-> current_post];
Return $ this-> post;
}
//...

} Function the_post (){
Global $ wp_query;

$ Wp_query-> the_post ();
}

Function have_posts (){
Global $ wp_query;

Return $ wp_query-> have_posts ();
}

Here we can clearly see the value assignment of $ post. Obviously, $ post belongs to one of the $ posts arrays.

The functions in the loop are also called $ wp_query member functions. Obviously, the_post () and $ post both come from the same object.

In this way, when this object is created, both of them already exist.

When I continued to search for the "Post =" string to confirm his assignment problem, I found that there were dozens of assignments in the entire WP, that is to say, $ post is often used for value assignment.

Although this does not completely prove that $ post has been assigned a value before the loop, it can at least prove that only the the_post () function in the loop can assign a value to $ post.

We have reason to believe that $ post has not been flushed out on the page, or when the program has not entered the main loop, it has been assigned a value.

========================================================== ==============================

Due to the wide and profound wp program and my personal talents, I have never been able to fully explain this contradiction.

At this point, although the heart is unwilling, I had to give up temporarily.

 

 

As for how $ post is assigned a value as an object (because $ post-> ID proves that $ post is an object), I can only find some relevant clues, it cannot be interpreted at all times.

Slightly recorded here:

We know that $ post is actually one of the $ posts arrays, so I started with $ posts and found the get_posts () function in the query. php file, where I found some value assignment statements.

Including:

Function & get_posts (){
//...
$ This-> posts = $ wpdb-> get_col ($ this-> request );
//...
$ This-> posts = $ wpdb-> get_results ($ this-> request );
//...
$ This-> posts = apply_filters_ref_array ('the _ posts', array ($ this-> posts, & $ this ));
//...
If ($ this-> post_count> 0 ){
$ This-> post = $ this-> posts [0];
}
//...
}

 

What we can be certain is that $ posts's assignment must be traced back to the db class in the wp-db.php (nonsense, haha), and some strange functions. (Maybe, while solving this mystery, we can also solve the contradictions I mentioned above)

In addition, the function & get_post (& $ post, $ output = OBJECT, $ filter = 'Raw') in post. php may be used to describe the construction of this OBJECT (without looking into it ).

At this point, I have to lament once again the wide and profound nature of wp, as well as the contempt for my own "man-in-the-box man" behavior.

 

This log ends.

I don't know when these questions will be solved or whether they can be solved.

I hope that you will be grateful if you can help me solve this problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.