简体   繁体   English

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

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

I need to query a WordPress site from Python 3 to see if a WordPress post of custom post type with a specific date and category already exists. 我需要从Python 3查询WordPress网站,以查看是否已存在具有特定日期和类别的自定义帖子类型的WordPress帖子。 Based on the docs for the the wp.GetPosts method, this seems to be possible, but I cannot work out the right query. 根据wp.​​GetPosts方法的文档 ,这似乎是可能的,但是我无法计算出正确的查询。 What I'm using currently is: 我目前正在使用的是:

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)

(where wp_blogid, wp_username and wp_password are all correctly defined, of course). (当然,其中正确定义了wp_blogid,wp_username和wp_password)。

What this returns is just a list of ten seemingly-random posts of the correct custom post type, with no further filter applied. 返回的内容只是正确的自定义帖子类型的十个看似随机的帖子的列表,没有应用进一步的过滤器。 Do I need to just get every post and then iterate over them for my terms, or is there an easier way? 我是否只需要获取每个帖子,然后按照我的条款对其进行遍历,还是有一种更简单的方法?

There is no option to filter posts by date, sorry. 很抱歉,无法按日期过滤帖子。 The documentation is misleading, in that it claims to support the same filters as #wp.getPost , but the latter has no filtering at all ; 该文档是误导性的,因为它声称支持相同的过滤器作为#wp.getPost ,但后者不具有滤波在所有 ; presumably that section confused fields with filters . 大概该节使过滤器混淆了字段

The documentation lists what is supported in the argument lists: 该文件列出了什么在参数列表的支持:

  • struct filter : Optional. struct filter :可选。
    • string post_type 字符串post_type
    • string post_status 字符串post_status
    • int number INT number
    • int offset 整数offset
    • string orderby 字符串orderby
    • string order 字符串order

The Wordpress XML-RPC source code handling the filters corroborates this. 处理过滤器Wordpress XML-RPC源代码证实了这一点。

So using XML-RPC, you have no other option but to page through the results to find the matching posts for the given date. 因此,使用XML-RPC,您别无选择,只能浏览结果以查找给定日期的匹配帖子。

You should look at the REST API instead, the /wp-json/wp/v2/posts route lets you filter posts by date. 您应该查看REST API/wp-json/wp/v2/posts路由可让您按日期过滤帖子。

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

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