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

How can i grab the ID of all posts?

最近进入 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')

According to the documentationWordPressPost 对象有一个 id 属性。打印 post 不会显示它,这可能就是您认为它不存在的原因?此代码对我有用:

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