在Python中使用XML-RPC 3、如何按日期和类别查看WordPress post是否存在?

Using XML-RPC in Python 3, how to see if a WordPress post exists by date and category?

我需要从 Python 3 查询一个 WordPress 站点,以查看是否已经存在具有特定日期和类别的自定义 post 类型的 WordPress post。基于 the docs for the the wp.GetPosts 方法,这似乎是可能的,但我无法计算出正确的查询。我目前使用的是:

import xmlrpc.client
wp_url = "http://www.mywebsite.com/xmlrpc.php"
server = xmlrpc.client.ServerProxy(wp_url)

filters = {
            'post_date': start_time, # an RFC-3339 formatted date
            'post_type' : 'my-custom-post-type', 
        }

fields = {
        'terms' : { 
            'category': ['my-category'] 
            }
        }

post_id = server.wp.getPosts(wp_blogid, wp_username, wp_password, filters, fields)

(当然,其中 wp_blogid、wp_username 和 wp_password 都已正确定义)。

这个 returns 只是一个看似随机的 post 正确自定义 post 类型的十个列表,没有应用进一步的过滤器。我是否只需要获取每个 post 然后根据我的条件对它们进行迭代,还是有更简单的方法?

抱歉,没有按日期过滤帖子的选项。该文档具有误导性,因为它声称支持 #wp.getPost 相同的过滤器,但后者 根本没有过滤 ;大概那个部分混淆了 fieldsfilters.

文档列出了参数列表中支持的内容:

  • struct filter: Optional.
    • string post_type
    • string post_status
    • int number
    • int offset
    • string orderby
    • string order

Wordpress XML-RPC source code handling the filters 证实了这一点。

因此使用 XML-RPC,您别无选择,只能翻阅结果以找到给定日期的匹配帖子。

你应该看看 REST API instead, the /wp-json/wp/v2/posts route 让你按日期过滤帖子。