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

To get the articles of a specified user (user_idThe list of all articles published, we need to usearchive/listThe interface is the core function of AnQi CMS used to retrieve document lists, which allows us to filter and sort through various conditions, including filtering by the author of the article.

Core function analysis:archive/listInterface

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

The API returns data where each article object contains auser_idField, this is the unique identifier of the article publisher. Its request parameters are designed very flexibly, although they are not listed directly in the documentuser_idAs an independent filter parameter, but in the description of 'Custom filter parameters', it is explicitly stated that we can filter any field existing in the document through the URL query parameters. Therefore, we can directlyuser_idPass as a request parameter toarchive/listinterface.

exceptuser_idSome other parameters are also very useful when building the request:

  • type: Recommended to be set aspage. WhentypeWithpageThe 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 example.limit=10means 10 articles per page.
  • page: WhentypeWithpageIt takes effect at this time, used to specify which page of content to retrieve.
  • order: Used to specify the sorting method of the article, for examplecreated_time descIt can bring the latest published articles to the forefront.

Practical Operation: Build Your API Request

Suppose we want to retrieveuser_idWith123The user has published all articles and wants to display 10 per page, sorted by publication time in descending order, and also get the content of the first page. So, we can build 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 replace{域名地址}with the actual domain name of your website.

The meaning of this request is:

  • Byuser_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 number.
  • 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 descMake sure the articles are arranged in chronological order from new to old.

Understand the returned data

After you send the above request, you will receive a JSON formatted response. A successful response iscodeThe field is usually0. The core content existsdatain the field, it 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 explicitly containsuser_idThe field verifies the accuracy of our filtering. At the same time,totalThe field will tell you the total number of articles that meet the criteria, which is very practical for displaying the total page number and navigation on the front-end page.

Application scenarios in practice

In 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 and pass in the author'suser_idDisplay all the articles contributed. CombinedlimitandpageParameter, can easily achieve pagination of the article list, improving user experience.

Furthermore, 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, displaying the most popular articles published by a specific user group.

Of Security CMSarchive/listThe interface provides us with great convenience and flexibility, making content management and display efficient and simple.


Frequently Asked Questions (FAQ)

1. How can I retrieve 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 their information,user_idYou may need to call other user-related APIs, such asapi/user/detailinterfaces, if the interface supports querying by username), or through any known article published by the userarchive/detailInterface, find it in the returned datauser_idField. Once obtaineduser_id, it can be applied toarchive/listInterface for filtering

2. Besidesuser_id, can I also use other conditions to filter the articles at the same time?Of course you can.archive/listThe interface supports combining multiple filtering conditions. For example, you can specifyuser_id/categoryId(Article Category ID),q(Search Keywords) andflag(Recommended attributes) and other parameters to achieve more refined article list filtering. Simply pass these parameters through&the symbol to connect in the URL.

3. How to handle the situation where no articles have been published?user_id?If the input includesuser_idthe corresponding user has not published any articles, or theuser_iddoes not exist,archive/listdata returned by the interface containsdatathe field will be an empty array[]at the same timetotalthe field will display0. Your front-end application can use this information to display friendly prompts to users such as 'This user has not published any articles'.