WordPress provides a rich Xmlrpc interface API for us to remotely manipulate WP content. The great open source community has been a package of these APIs, providing a more complete Python library, the use of the document address http://python-wordpress-xmlrpc.readthedocs.org/, the document content is very comprehensive. I will summarize some of the contents of my experiment here.
WordPress has a revision mechanism, causing us to update the document through the API interface, we will save 2 records in the database, one is the body, and the other records such as Id-revision, no use for me, so first through the code to disable.
Define false);
In this Python class library, the properties of the Wordpresspost object are implemented by SetAttr.
Query post
The API can be used to obtain the relevant information for the specified post, and what information can be found in the table below which the post is added. A simple code example to get a post list is as follows:
Def testgetpost (): = Wp_server.call (wordpress_xmlrpc.methods.posts. Getposts ()) for W in WP: print W.title Print w.post_status
In addition, the Getposts function accepts different parameters for conditional queries, for example, the following statement returns the last updated 100 post
Recently_modified = Client.call (posts. Getposts ({' by ': ' post_modified ', ' Number ': 100})
The following code returns the post entry for the specified number of bars starting with offset:
Posts = Client.call (posts. Getposts ({' Number ': Increment, ' Offset ': offset})
New post
New post Duchy Wordpress_xmlrpc.methods.posts.NewPost (Wordpresspost) to create, here is a brief new post code snippet.
Post = wordpresspost () post. title = "Test4" post. date_modified = datetime.datetime. Now () post. Content = "All English interface, but there is unofficial Chinese translation." It is recommended to read the original English text directly, both accurate and reliable and can improve English proficiency. The way to simulate the formal competition, using standard evaluation machine, file input and output, direct submission program source file evaluation method. " post. Excerpt =" Full English interface, but with unofficial Chinese translation " post. Post_status =" Publish " print Wp_server.call (Wordpress_xmlrpc.methods.posts.NewPost (POST))
The parameters that are supported when inserting are:
Fields in Python |
Xmlrpc corresponding fields |
Meaning |
Date |
Post_date_gmt |
|
Date_modified |
Post_modified_gmt |
Modification time of Post |
Slug |
Post_name |
|
Post_status |
Post_status |
Post status, optional draft, publish, common publish can be published directly post |
Title |
|
Post title |
Content |
Post_content |
Post Content |
Excerpt |
Post_excerpt |
Summary |
Link |
|
|
Comment_status |
|
|
Ping_status |
|
|
Terms |
|
|
Terms_names |
|
|
Custom_fields |
|
|
Enclosure |
|
|
Post_format |
|
|
Thumbnail |
Post_thumbnail |
|
Sticky |
|
Sticky display, setting true |
Post_type |
|
The type of post, the default is post, or it can be a page, |
parent_id |
Post_parent |
ID of the parent article |
Menu_order |
|
|
Guid |
|
|
Mime_type |
Post_mime_type |
|
Set Post's category and tag, Post's Terms property corresponds to wordpressterm object. If you want to add a term, you can use the following code to
Post = wordpresspost ()post. title = ' Post with new tags 'post. Content = ' ... ' Post.terms_names = { ' post_tag ': [' Taga ', ' another tag '], ' category ': [' My child category '],}post. id = Client.call (posts. Newpost (POST))
Edit Post
Edit the Post that has been published, basically the same as the new one, or depending on the Wordpresspost structure class
Def testmodifypost (): = Wp_server.call (wordpress_xmlrpc.methods.posts.GetPost (1)) WP True print wp_server.call (wordpress_xmlrpc.methods.posts.EditPost (wp.id, WP))
The above code will display the post PostID is 1.
WordPress Learning Five: remote operation of WordPress via WORDPRESS_XMLRPC python Package