简体   繁体   English

如何使用 WordPress REST API v2 搜索帖子?

[英]How to use WordPress REST API v2 to search posts?

I am writing Android application and I am using WordPress REST API v2 for communication with WordPress from my Android application.我正在编写 Android 应用程序,并且正在使用 WordPress REST API v2 从我的 Android 应用程序与 WordPress 进行通信。 I have implemented Activity that lists all posts from single category:我已经实现了列出来自单个类别的所有帖子的活动:

http://wordpress.dev/wp-json/wp/v2/posts?categories=X

But now I am adding search box in my application where user can enter some text and now I need to use this text to perform search over all categories但是现在我在我的应用程序中添加搜索框,用户可以在其中输入一些文本,现在我需要使用此文本对所有类别执行搜索

I can use same endpoint but different parameters:我可以使用相同的端点但不同的参数:

http://wordpress.dev/wp-json/wp/v2/posts?search=<SEARCH TEXT>

In my local wordpress database I have a lot of posts that are called "POST TITLE #XY" and one that is called "Cover Photo"在我的本地 wordpress 数据库中,我有很多帖子被称为“POST TITLE #XY”和一个被称为“封面照片”

So if I execute this:所以如果我执行这个:

http://wordpress.dev/wp-json/wp/v2/posts?search=title,post

I get only those that has post and title in the title, however if I add one more word:我只得到标题中有 post 和 title 的那些,但是如果我再添加一个词:

http://wordpress.dev/wp-json/wp/v2/posts?search=title,post,cover

I get zero posts...我的帖子为零...

How can I perform search and get posts that includes one or more keywords that I am sending in request?如何执行搜索并获取包含我在请求中发送的一个或多个关键字的帖子?

I did something similar to what you are describing for a WP/bbPress android/ios app.我做了一些类似于您为 WP/bbPress android/ios 应用程序所描述的操作。 First, I made my own wordpress search api end point to hit.首先,我创建了自己的 wordpress 搜索 api 端点来命中。 Then on the wordpress side I used a custom mysql query to query post titles and post content for the search string and return that to the api.然后在 wordpress 方面,我使用自定义 mysql 查询来查询搜索字符串的帖子标题和帖子内容,并将其返回给 api。

I made the query only return 25 results as I didn't need to have more results than that, with pagination and such.我让查询只返回 25 个结果,因为我不需要更多的结果,比如分页等等。

You can use Search Results REST API Endpoint to seach by keywords you provide.您可以使用搜索结果REST API 端点按您提供的关键字进行搜索。

For example http://yourdomain.com/index.php/wp-json/wp/v2/search?search=lorem&per_page=5&page=1 will search for the keyword lorem in your wordpress posts.例如http://yourdomain.com/index.php/wp-json/wp/v2/search?search=lorem&per_page=5&page=1将在您的 wordpress 帖子中搜索关键字lorem

Per_page : limits the number of results per page Per_page : 限制每页的结果数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM