如何在 Python 中按类别获取 WordPress post 自定义 post 类型
How to get WordPress posts of custom post type by category in Python
我正在按照文档 here 按类别查询自定义 post 类型的 WordPress post。这个问题有 PHP 的答案,但是 none 我可以找到 Python 的答案。除非我遗漏了什么,否则该功能不会按预期工作。如果我从以下内容开始:
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc import WordPressTerm
wp = Client('http://www.mywebsite.info/xmlrpc.php', 'myusername', 'mypassword')
然后这成功 returns 有问题的类别列表:
categories = wp.call(taxonomies.GetTerms('category'))
这成功 returns 我正在尝试查询的自定义 post 类型的(前 10 个)post 列表:
posts = wp.call(posts.GetPosts({ 'post_type': 'my-custom-post-type' }))
但如果我添加以下内容:
posts = wp.call(posts.GetPosts({ 'post_type': 'my-custom-post-type', 'terms_names': { 'category' :['my-category',]} }))
它仍然给出与没有 'terms-names' 过滤器的查询相同的结果,就好像完全忽略了附加词一样。事实上,如果我将 nonexistent 类别添加到查询中,它会给出相同的结果而不返回 exception/error,例如:
posts = wp.call(posts.GetPosts({ 'post_type': 'my-custom-post-type', 'terms_names': { 'category' :['nonsense-word',]} }))
我也尝试过其他方法,例如:
posts = wp.call(posts.GetPosts({ 'post_type': 'my-custom-post-type', 'terms': { 'category' :[72]} }))
...结果相同。我在这里不明白什么?
如果您阅读文档,它似乎没有给出任何复杂的查询示例。当我去 GitHub 存储库时,我看到这是一些开发人员的个人项目,只测试了 WordPress 3.5(4.9 现在即将推出),最后一次提交似乎是 2 年前。 WordPress 是用 PHP 编写的。我认为作者只是没有在 Python 中经历过实现 WP 查询的全部功能的麻烦。如果可能的话,我会使用 WordPress Rest API。
也就是说,由于它允许您按 post 类型检索,如果返回的 post 数据包含类别信息,您可以自己进行筛选。
我正在按照文档 here 按类别查询自定义 post 类型的 WordPress post。这个问题有 PHP 的答案,但是 none 我可以找到 Python 的答案。除非我遗漏了什么,否则该功能不会按预期工作。如果我从以下内容开始:
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc import WordPressTerm
wp = Client('http://www.mywebsite.info/xmlrpc.php', 'myusername', 'mypassword')
然后这成功 returns 有问题的类别列表:
categories = wp.call(taxonomies.GetTerms('category'))
这成功 returns 我正在尝试查询的自定义 post 类型的(前 10 个)post 列表:
posts = wp.call(posts.GetPosts({ 'post_type': 'my-custom-post-type' }))
但如果我添加以下内容:
posts = wp.call(posts.GetPosts({ 'post_type': 'my-custom-post-type', 'terms_names': { 'category' :['my-category',]} }))
它仍然给出与没有 'terms-names' 过滤器的查询相同的结果,就好像完全忽略了附加词一样。事实上,如果我将 nonexistent 类别添加到查询中,它会给出相同的结果而不返回 exception/error,例如:
posts = wp.call(posts.GetPosts({ 'post_type': 'my-custom-post-type', 'terms_names': { 'category' :['nonsense-word',]} }))
我也尝试过其他方法,例如:
posts = wp.call(posts.GetPosts({ 'post_type': 'my-custom-post-type', 'terms': { 'category' :[72]} }))
...结果相同。我在这里不明白什么?
如果您阅读文档,它似乎没有给出任何复杂的查询示例。当我去 GitHub 存储库时,我看到这是一些开发人员的个人项目,只测试了 WordPress 3.5(4.9 现在即将推出),最后一次提交似乎是 2 年前。 WordPress 是用 PHP 编写的。我认为作者只是没有在 Python 中经历过实现 WP 查询的全部功能的麻烦。如果可能的话,我会使用 WordPress Rest API。
也就是说,由于它允许您按 post 类型检索,如果返回的 post 数据包含类别信息,您可以自己进行筛选。