简体   繁体   中英

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. Based on the docs for the the wp.GetPosts method, this seems to be possible, but I cannot work out the right query. 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).

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 ; presumably that section confused fields with filters .

The documentation lists what is supported in the argument lists:

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

The Wordpress XML-RPC source code handling the filters corroborates this.

So using XML-RPC, you have no other option but to page through the results to find the matching posts for the given date.

You should look at the REST API instead, the /wp-json/wp/v2/posts route lets you filter posts by date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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