我如何获取所有帖子的ID?

时间:2017-03-30 17:25:16

标签: python wordpress xml-rpc

最近进入了python中的XML-RPC库,我需要获取wordpress网站上所有帖子的ID。我尝试使用EditPost()命令,但似乎需要帖子的ID。

在以下代码中,我尝试将所有帖子更改为草稿

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc import WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
from wordpress_xmlrpc.methods.posts import EditPost

wp = Client('website-link', 'x', 'x')

post = WordPressPost()
#Posts returns the name of the posts not IDs
posts = wp.call(posts.GetPosts())
print(posts)


for post in posts:
    post.post_status = 'draft'
    wp.call(EditPost(post,post))
print('done')

1 个答案:

答案 0 :(得分:2)

According to the documentationWordPressPost对象具有id属性。打印帖子不会显示它,这可能是为什么你认为它不存在?这段代码对我有用:

from wordpress_xmlrpc import Client
from wordpress_xmlrpc.methods.posts import GetPosts
wp = Client("https://wordpress.example.com/xmlrpc.php", "admin", "password")
allposts = wp.call(GetPosts())
for thepost in allposts:
    print thepost.id

因此,您的代码应该通过更改循环来实现,如下所示:

for post in posts:
    post.post_status = 'draft'
    wp.call(EditPost(post.id, post))