python wordpress xmlrpc 调用_20542通过wordpress_xmlrpc的python包远程操作wordpress 编辑 更新Post...

wordpress提供了丰富的xmlrpc接口api来供我们远程操控wp的内容。伟大的开源社区有人就将这些api做了一下封装,提供了一个功能比较完整的http://python-wordpress-xmlrpc.readthedocs.org/ , 文档内容还是很全面的。这里我将自己试验的一些内容做一下总结。

define('WP_POST_REVISIONS', false);

1

define('WP_POST_REVISIONS',false);

在这个

查询Post

利用api可以获取指定的post的相关信息,具体有哪些信息可以参看下面新增post的那个表。一个简单的获取post列表的代码例子如下:

def TestGetPost():

wp = wp_server.call(

1

2

3

4

5

defTestGetPost():

wp=wp_server.call(.methods.posts.GetPosts())

forwinwp:

printw.title

printw.post_status

此外,GetPosts函数接受不同的参数来进行条件查询, 例如下面的的语句返回最近更新的100个post

recently_modified = client.call(posts.GetPosts({'orderby': 'post_modified', 'number': 100}))

1

recently_modified=client.call(posts.GetPosts({'orderby':'post_modified','number':100}))

下面的代码是返回从offset开始的指定条数的post条目:

posts = client.call(posts.GetPosts({'number': increment, 'offset': offset}))

1

posts=client.call(posts.GetPosts({'number':increment,'offset':offset}))

新建Post

新建的post公国wordpress_xmlrpc.methods.posts.NewPost(WordPressPost)来创建,以下是一个简短的新建一个Post的代码段。

post = WordPressPost()

post.title = "test4"

post.date_modified = datetime.datetime.now()

post.content = "全英文界面,但有非官方的中文翻译。推荐直接阅读英语原文,既准确可靠又可提高英语水平。做题方式模拟正式比赛,采用标准测评机、文件输入输出、直接提交程序源文件的测评方式。"

post.excerpt = "全英文界面,但有非官方的中文翻译"

post.post_status = "publish"

print wp_server.call(

1

2

3

4

5

6

7

post=WordPressPost()

post.title="test4"

post.date_modified=datetime.datetime.now()

post.content="全英文界面,但有非官方的中文翻译。推荐直接阅读英语原文,既准确可靠又可提高英语水平。做题方式模拟正式比赛,采用标准测评机、文件输入输出、直接提交程序源文件的测评方式。"

post.excerpt="全英文界面,但有非官方的中文翻译"

post.post_status="publish"

printwp_server.call(.methods.posts.NewPost(post))

插入时支持的参数有:

python中字段

xmlrpc对应字段

含义

date

post_date_gmt

date_modified

post_modified_gmt

post的修改时间

slug

post_name

post_status

post_status

post的状态, 可选draft、publish,常用publish就可以直接发布post了

title

post标题

content

post_content

post 内容

excerpt

post_excerpt

摘要

link

comment_status

ping_status

terms

terms_names

custom_fields

enclosure

post_format

thumbnail

post_thumbnail

sticky

置顶显示, 设置True

post_type

post的类型, 默认为post, 也可以为page,

parent_id

post_parent

上级文章的id

menu_order

guid

mime_type

post_mime_type

设置post的category和tag, Post的terms属性对应 WordPressTerm 对象。如果要新增一个term,可以用下面的代码来进行

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))

1

2

3

4

5

6

7

8

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))

编辑Post

编辑已经发布的Post,和新建基本上一样, 还是依赖于WordPressPost结构类

def TestModifyPost():

wp = wp_server.call(wordpress_xmlrpc.methods.posts.GetPost(1))

wp.sticky = True

print wp_server.call(wordpress_xmlrpc.methods.posts.EditPost(wp.id, wp))

1

2

3

4

defTestModifyPost():

wp=wp_server.call(wordpress_xmlrpc.methods.posts.GetPost(1))

wp.sticky=True

printwp_server.call(wordpress_xmlrpc.methods.posts.EditPost(wp.id,wp))

上面的代码将postid是1的post进行置顶显示。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值