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.