In website content operation, we often need to display all articles by a specific author, such as on the author's personal homepage, or on a special topic page aggregating the content of specific contributors.AnQiCMS provides a powerful and flexible API interface, allowing us to easily achieve this requirement.

To get the articles of a specified user (user_idAll articles released, we need to usearchive/listInterface.This interface is the core feature of Anqi CMS used for retrieving document lists, which allows us to filter and sort by various conditions, including filtering by the article publisher.

Core Function Analysis:archive/listInterface

archive/listThe API call address is usually{域名地址}/api/archive/list, its call method isGETRequest. The key to understanding this interface is its request parameters, especially those that can help us accurately locate the filtering conditions for articles.

Each article object in the return data of this interface contains auser_idField, this is the unique identifier of the article publisher. The request parameters are designed to be very flexible, although they are not directly listed in the documentuser_idAs an independent filter parameter, but it is explicitly pointed out in the description of "Custom Filter Parameters" that we can filter any field existing in the document through the query parameters of the URL. Therefore, we can directly putuser_idPassed as a request parameterarchive/listinterface.

Exceptuser_idThere are also some other parameters that are very useful when building the request:

  • type: Recommended settingpage). Whentyperesponse forpageThe interface will return the total number of articles whentotalThis is crucial for building pagination functionality.
  • limit: It is used to control the number of articles displayed per page. You can set it as needed, for examplelimit=10This means 10 articles are displayed per page.
  • pageWhentyperesponse forpage[en]Effective immediately, used to specify which page of content to retrieve.
  • order[en]Used to specify the sorting method of the article, for example,created_time desc[en]It can make the latest published articles appear at the top.

Practice Operation: Build Your API Request

Suppose We Want to Getuser_idresponse for123The user has published all articles, and hopes to display 10 articles per page, sorted by publish time in descending order, while retrieving the content of the first page. Then, we can construct the API request like this:

GET {域名地址}/api/archive/list?user_id=123&type=page&limit=10&page=1&order=created_time desc

Please remember to set{域名地址}replace it with your actual website domain.

The meaning of this request is:

  • Passuser_id=123precisely filter out the articles published by user ID 123.
  • type=pageTell the system that we need to perform pagination queries and return the total count.
  • limit=10Specify that 10 articles should be returned per page.
  • page=1Indicates that we are currently requesting the articles for the first page.
  • order=created_time descEnsure that the articles are arranged in chronological order from new to old.

Understand the returned data.

After you send the aforementioned request, you will receive a JSON-formatted response. A successful response is:codeThe field is usually for0. The core content will exist indatafield, which is an array containing article objects.

{
  "code": 0,
  "data": [
    {
      "id": 101,
      "title": "用户123发布的第一篇文章",
      // ... 其他文章信息 ...
      "user_id": 123,
      "created_time": 1700000000
    },
    {
      "id": 102,
      "title": "用户123发布的第二篇文章",
      // ... 其他文章信息 ...
      "user_id": 123,
      "created_time": 1699900000
    }
    // ... 更多文章 ...
  ],
  "total": 50, // 假设用户123共有50篇文章
  "msg": ""
}

EachdataEach element in the array represents a document, which will clearly includeuser_idField verifies the accuracy of our filtering. At the same time,totalField tells you the total number of articles that meet the criteria, which is very practical for displaying the total number of pages and navigation on the front-end page.

Actual application scenarios

This way, you can create a dedicated article list page for the contributors or authors of the website. When a user visits a specific author's personal homepage, you can dynamically call this API, passing in the author'suser_id[en] Show all the articles of the contribution. Combinedlimitandpage[en] With the parameter, it can easily achieve pagination of the article list, enhancing the user experience.

In addition, you can also apply this method to more complex scenarios, such as:

  • In the 'My Posts' feature, allow users to view all the articles they have published.
  • Build a content aggregation page to display popular articles published by a specific user group.

Anqi CMS'sarchive/listThe interface provides us with great convenience and flexibility, making content management and display efficient and simple.


Common Questions (FAQ)

1. How can I get their information if I only know the user's nickname or username?user_id?If you only know the user's nickname or username, and do not have direct access to theiruser_idYou may need to call other user-related APIs first (such asapi/user/detailan interface, if the interface supports querying by username), or through any known article published by the user.archive/detailInterface, find it in the returned data.user_idField. Once obtained,user_idit can be applied toarchive/listInterface for filtering.

2. Besidesuser_idCan I also use other conditions to filter the articles at the same time?Of course you can.archive/listInterface supports combining multiple filtering conditions. For example, you can specifyuser_id/categoryId(Article Category ID),q(Search Keywords) as well asflag[Recommended attribute] and other parameters to achieve more refined article list filtering. Simply pass these parameters through&symbols connected in the URL.

How to handle a situation whereuser_id?if传入的user_idthe corresponding user has not published any articles, or theuser_iddoes not exist,archive/listreturn data of the interface containsdataThe field will be an empty array[]as well astotalthe field will display0. Your frontend application can use this information to display friendly prompts to the user, such as 'This user has not published any articles'.