【问题标题】:How can i grab the ID of all posts?如何获取所有帖子的ID?
【发布时间】:2017-03-30 17:25:16
【问题描述】:

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

【问题讨论】:

标签: python wordpress xml-rpc


【解决方案1】:

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

【讨论】:

  • 完成了 miken32,非常感谢您的澄清!
  • 对于未来的访问者可能值得一提的是,根据the documentationGetPosts 只返回最近的十个帖子。
猜你喜欢
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
相关资源
最近更新 更多