Wp_query () class in WordPress tutorial

Source: Internet
Author: User
How to Use wp_query

Before talking about wp_query, we need to distinguish two terms:

  • Wp_query is a WordPress built-in class for processing complex requests (the request content here includes not only articles, but also pages, users, categories and other information ).
  • $ Wp_query is a wp_query entity object defined in the wp-blog-header.php file that provides information about the current request.

When you access the WordPress homepage or category page, WordPress creates a wp_query instance by default as the main cycle of the page. When viewing the WordPress topic source code, you often see code similar to: While (have_posts (): the_post. This is how to check whether there are articles in the main cycle of this page and display them. However, in many cases, in addition to the main cycle of the page, we also need to create our own query objects, for example, to display popular articles and specific articles of someone.

Here we will mainly discuss the methods and attributes of the wp_query class. In the official documents, the wp_query attribute is preferred, but I think it is easier for users to understand the method.

Method

The wp_query class accepts a string or array-type query condition as its constructor. Besides its constructor, wp_query also contains 13 functions. Their functions are as follows:

Init ():Class internal initialization function (note, not constructor), used to initialize the object, clear all attributes (set to null, 0 or false ).

Parse_query ($ query ):Parse_query accepts a string type query string and assigns the parsed parameters to the class itself. When a parameter of the string type is input in the constructor, this method is called and the parameter of the string type is converted to an array structure.

Parse_query_vars (): it does not actually work, but it re-parses the previous query function. This is equivalent to calling parse_query without parameters. (In fact, it is implemented in the source code ).

Get ($ query_var), set ($ query_var, $ value ):Set or obtain an attribute in the query condition separately. If you don't want to write a long string of difficult-to-read query conditions, try the set function.

& Query ($ query), & get_posts ():Query is used to accept query conditions and find the corresponding content from the database. If the content is an article, the second method get_posts can be used to return these articles. Note that when get_posts is called, the $ posts and $ post_count parameters in the object are changed.

Have_posts (), the_post (), next_post (), rewind_posts ():Wp_query creates a queue when querying. The queue contains all the articles found. These four methods are used to operate the queue. The name is clear at a glance. have_posts checks whether there are any articles in the queue. If there are any articles in the queue, the_post gets the current article. next_post allows the queue to move one to the back, and rewind_posts is the beginning of the returned message to the queue.

Get_queried_object (), get_queried_object_id ():As we mentioned at the beginning, wq_query is not only used to query articles and pages, but also to query users, categories, and other information. These two methods are used to determine the currently queried object and return the ID of the object or object.

Attribute

After learning about the above method, it is not difficult to understand the attributes.

$ Query, $ query_vars:The query condition of wp_query. $ Query is a string, and $ query_vars is the parsed array format.

$ Queried_object, $ queried_object_id:Corresponds to the get_queried_object () and get_queried_object_id () methods. It may be articles, pages, users, and categories.

$ Posts:All articles found.

$ Post_count, $ found_posts, $ max_num_pages:The total number of articles, the number of queried articles, and the total number of pages respectively.

$ Current_post, $ post:The number of sequences in the current article and the current post object.

Other attributes include: $ 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, $ response, $ is_trackback, $ is_home, $ is_404, $ response, $ is_admin, $ is_attachment, $ is_singular, $ is_robots, $ is_posts_page, $ is_paged. Through its name, we can basically see its meaning, so we will not explain it much.

Instance

As described above, it may be difficult for you to know how to use wp_query. Let's use several instances to better understand it.

Obtain articles by author

Query the articles of a single author

Search by ID.

$query = new WP_Query( ‘author=123‘ );

Search by user name.

$query = new WP_Query( ‘author_name=rami‘ );

Query articles from multiple people

$query = new WP_Query( ‘author=2,6,17,38‘ );

Query articles that do not belong to someone

You can exclude an author by minus.

$query = new WP_Query( ‘author=-12‘ );

Search by category directory

Query articles under a specific category

The following parameters may be used in a query statement: CAT, category _ name, category _ and, category _ And, category _ in, and category _ not_in. Example:

Query articles under a certain category (including its subcategories)

$ Query = new wp_query ('cat = 4'); // by category ID
$ Query = new wp_query ('category _ name = Staff '); // press slug

Query articles under a certain category (excluding its subcategories)

$query = new WP_Query( ‘category__in=4‘ );

Similarly, query articles under multiple categories

$query = new WP_Query( ‘cat=2,6,17,38‘ ); // ID
$query = new WP_Query( ‘category_name=staff,news‘ ); // slug

Does not contain a category

$query = new WP_Query( ‘cat=-12,-34,-56‘ );

Query articles that belong to multiple categories at the same time

The following statement is used to query the articles that contain both 2 and 6 categories. (Note: Category _ in is a "or" relation, and cateory _ and is a "and" relation.

$query = new WP_Query( array( ‘category__and‘ => array( 2, 6 ) ) ); // 2 and 6
$query = new WP_Query( array( ‘category__in‘ => array( 2, 6 ) ) ); // 2 or 6

Similarly, the following statements are not difficult to understand. Does not contain articles with the ID of 2 or 6.

$query = new WP_Query( array( ‘category__not_in‘ => array( 2, 6 ) ) );

Query by tag

Tag query conditions include tag, tag_id, tag _ And, tag _ in, tag _ not_in, tag_slug _ and, and tag_slug _ in.

It is difficult to understand how to use each condition according to the query method of the preceding classification. I will not give an example here. (Note: If the name does not contain slug, use the tag ID to query it ).

Taxonomy

Taxonomy is a general classification. Both tag and category can be included in taxonomy. In fact, tag and category are also implemented using taxonomy. You can also create your own set of taxonomy, if you are interested, we can continue to discuss taxonomy in future articles. With taxonomy, you can create various query functions that suit your needs. Here, we only use a complex example to illustrate:

$args = array(
‘post_type‘ => ‘post‘,
‘tax_query‘ => array(
‘relation‘ => ‘OR‘,
array(
‘taxonomy‘ => ‘category‘,
‘field‘ => ‘slug‘,
‘terms‘ => array( ‘quotes‘ )
),
array(
‘taxonomy‘ => ‘post_format‘,
‘field‘ => ‘slug‘,
‘terms‘ => array( ‘post-format-quote‘ )
)
)
);
$query = new WP_Query( $args );

In this example) post-format-quote ('Post _ type' => 'post ').

Search

Wp_query also provides a simple keyword-based search function. See the following example:

$query = new WP_Query( ‘s=keyword‘ );

However, WordPress only matches strings and does not expect the same search results as the search engine.

Wp_query () class in WordPress tutorial

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.