How to get the author and category information in WordPress

Source: Internet
Author: User
This article mainly introduces the methods used in WordPress to obtain the author and category information. they are all from the WP_Query class of WordPress. For more information, see Author
You can use four parameters to query articles related to some authors (users:

  1. Author (integer): User ID
  2. Author_name (string): User's nickname ("user_nicename" field)
  3. Author _ in (array): User ID
  4. Author _ not_in (array): User ID

Get an author's article

Get by user ID:

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

Obtain the following information based on the user's nickname ("user_nicename" field:

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

Obtain articles from multiple authors

Obtain the articles of multiple authors based on the user ID:

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

Exclude the author's article

To exclude an author, add "-" to the front of the user ID:

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

Multi-Author Query

Get articles from multiple authors:

$query = new WP_Query( array( 'author__in' => array( 2, 6 ) ) );

Exclude articles from multiple authors:

$query = new WP_Query( array( 'author__not_in' => array( 2, 6 ) ) );

Category

There are five available parameters for classification:

  1. Cat (integer): Category ID
  2. Category_name (string): Category alias
  3. Category _ and (array): category ID
  4. Category _ in (array): category ID
  5. Category _ not_in (array): category ID

Get an article by category

Obtain an article by Category ID (including the subcategory of this category ):

$query = new WP_Query( 'cat=4' );

Obtain an article by category alias (including the subcategory of this category ):

$query = new WP_Query( 'category_name=staff' );

Obtain an article by Category ID (excluding the subcategory of this category ):

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

Get articles of multiple categories

Get articles of multiple categories based on the Category ID:

$query = new WP_Query( 'cat=2,6,17,38' );

Obtain articles of multiple categories based on the category alias:

$query = new WP_Query( 'category_name=staff,news' );

Get articles with several categories based on the category alias:

$query = new WP_Query( 'category_name=staff+news' );

Exclude Category

To exclude some categories, you only need to add the "-" before the Category ID:

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

Multi-category query

Obtain an article with multiple categories at the same time. the code below will obtain an article with both IDs 2 and 6:

$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );

An article that retrieves multiple categories does not contain its subcategories. the following code obtains an article with a classification of ID 2 or ID 6:

$query = new WP_Query( array( 'category__in' => array( 2, 6 ) ) );

Exclude some classified articles:

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

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.