This article mainly introduces the Loop structure and usage analysis of the WordPress Query article, along with some basic information about the WP_Query class that provides various query methods, if you need it, you can refer to the WordPress document. The most important thing is Loop. In fact, Loop is to query the corresponding article in the database and store it in the global variable temporarily, when necessary, an article is output, and WordPress's cycle design is very good. to complete a loop, you need to execute more than 2000 lines of code, what you can see when using the loop is a simple while loop with several functions. it is also easy for beginners to understand.
<?phpif( have_posts() ): while( have_posts() ): the_post(); endwhile;endif;?>
The above is a common main loop structure. First, use the have_posts () function to determine whether there is an article. If yes, use the while statement to loop and call the_posts () function in the loop body, the_posts () the function is used to tell the WordPress core query class. I have checked an article. The current article in the loop changes every time the_posts () is called.
After calling the_posts () several times and the article has completed all the loops, let the have_posts () function return False to end the loop. Therefore, a common loop should be like this:
<?php if( have_posts() ): ?>
<?php while( have_posts() ):the_post();?>
- > <?php the_title( '', '' ); ?>
<?php endwhile; ?>
<?php endif; ?>
WP_Query class
The methods used in the loop are all from the WP_Query class. here, let's take a look at the WP_Query class:
WP_Query functions include processing query conditions, getting articles from the database, sorting and storing articles, article loops, and determining page types based on the article query conditions.
Attribute
$ Query
The input query conditions.
$ Query_vars
Query conditions available for processing $ query.
$ Queried_object
The query attribution is based on the query type. For example, if you query by category, a class containing category information is returned; if it is a tab, a class containing tag information is returned; the author page is similar.
$ Queried_object_id
Returns the ID of $ queried_object. for example, the category is the category ID, the tag is the tag ID, and so on.
$ Posts
The articles and article information queried from the database are stored in this variable in an array. each article is an object instantiated from the WP_Post class.
$ Post_count
The number of current articles. In other words, several articles are stored in the $ posts variable.
$ Found_posts
Count how many articles are there if pagination is not enabled.
$ Max_num_pages
Total number of pages. The current article is divided into several pages.
$ Current_post
The index value of the current article. In a loop, the starting loop $ current_post is-1, and 1 is added for each loop. this indicates the index value of the current loop to the article in the $ posts variable (array.
$ Post
The current article in the loop is an object that contains the article and article information instantiated through the WP_Post class.
$ Is_single, $ is_page, $ is_archive, $ is_preview, $ is_date, $ is_year, $ is_month, $ is_time, $ is_author, $ is_category, $ is_tag, $ is_tax, $ is_search, $ is_feed, $ is_comment_feed, $ is_trackback, $ is_home, $ is_404, $ region, $ is_admin, $ is_attachment, $ is_singular, $ is_robots, $ is_posts_page, $ is_paged
Determine the current page based on the query conditions and store it in some variables. for details, see WordPress page judgment function.
Method
Init ()
Initialize the object. all attributes are set to null,-1, 0, or False to delete some attributes.
Parse_query ($ query)
Parse and save the query string.
Parse_query_vars ()
In the previous versions of WordPress, parse_query () is the same as parse_query (). now you can directly call parse_query.
Get ($ query_var)
Obtain a query condition by name.
Set ($ query_var, $ value)
Set a query condition.
Get_posts ()
Obtain articles from the database based on the existing query conditions, save them in the $ posts attribute, and set the $ post_count attribute. The returned value of this function is the $ posts attribute of the document.
Next_post ()
(Loop control) add $ current_post to 1, that is, push the current article forward, add the current article after the promotion to the $ post attribute, and then return the $ post attribute.
The_post ()
(Loop control) go to the next article, call the next_post () function, and import the promoted article to the global variable.
Have_posts ()
(Loop control) checks whether the loop is completed. if all articles have been completed or no articles exist, False is returned.
Rewind_posts ()
(Loop control) jump to the beginning of the loop. This is equivalent to resetting the $ current_post and $ post attributes.
Query ($ query)
Query articles, including calling the init () method, parse_query () method, and get_posts () method, and returning the return value of the get_posts () method, this function can be used to complete the query of an article.
Get_queried_object ()
Generate the $ queried_object attribute. if the attribute already exists, $ queried_object is directly returned. Therefore, because $ queried_object does not exist by default, you must directly use the get_queried_object () method to call the $ queried_object attribute.
Get_queried_object_id ()
Generate the $ queried_object_id attribute. if the attribute already exists, the $ queried_object_id attribute is directly returned. Similar to the get_queried_object () method.
WP_Query ($ query = ''), _ constructor ($ query = '')
WP_Query constructor is called by yourself during class instantiation. If you input a query condition, it will automatically call the query () method to help you query the article.