获取所有帖子通过 xmlrpc 请求从 WordPress 博客获取 ID

发布于 2024-09-27 03:32:43 字数 113 浏览 3 评论 0原文

我没有在 WordPress Codex 中找到任何 xmlrpc 方法调用来执行此操作。 我可以通过metaWeblog.getRecentPosts 获取所有帖子并提取ID,但我并不确切知道博客中帖子的数量。

I didn't find any xmlrpc method call to do it in WordPress codex.
I can get all the posts via metaWeblog.getRecentPosts and extract IDs, but I didn't exactly know the count of the posts in blog.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赤濁 2024-10-04 03:32:43

没有一种 XML-RPC 方法可以获取所有帖子,主要是因为这可能会导致严重的性能问题(想象一个具有 5,000 个帖子和高流量的博客......尝试解析所有内容的列表会导致一些严重的服务器延迟)。

使用 WordPress 常用方法最接近的方法是使用 getRecentPosts 调用:blogger.getRecentPostsmetaWeblog.getRecentPosts (MetaWeblog 调用实际上是只是 Blogger 调用的别名)。

也就是说,您可以创建自己的方法来返回已发布帖子的计数或已发布帖子的 ID 列表。只需创建一个快速插件来连接到 XML-RPC 系统即可添加端点和方法:

function xml_add_method( $methods ) {
    $methods['myNamespace.postCount'] = 'get_post_count';
    $methods['myNamespace.postIDList'] = 'get_post_id_list';
    return $methods;
}
add_filter( 'xmlrpc_methods', 'xml_add_method' );

该代码块将向您的 XML-RPC 系统添加两个新调用:myNamespace.postCountmyNamespace.postIDList。您可以远程调用它们以分别返回已发布帖子的计数和已发布帖子 ID 的列表。

您还需要定义将处理请求的 PHP 函数 - 所有 XML-RPC 系统都会将远程请求路由到返回数据的内部 PHP 函数:

function get_post_count( $args ) {
    global $wpdb;

    ... code that retrieves the total count of published posts from the database ...

    return $count;
}

function get_post_id_list( $args ) {
    global $wpdb;

    ... code that retrieves a list of published posts from the database ...

    return $postlist;
}

就是这样。将所有代码集中到一个自定义插件中,将其放置在您的站点中,激活它,您现在可以通过 XML-RPC 获取已发布帖子的计数或已发布帖子 ID 的列表。

There isn't an XML-RPC method to get all posts, mostly because that could lead to significant performance issues (imaging a blog with 5,000 posts and high traffic ... trying to parse a list of everything would cause some serious server lag).

The closest you can get with stock WordPress methods would be with the getRecentPosts calls: blogger.getRecentPosts and metaWeblog.getRecentPosts (the MetaWeblog call is actually just an alias to the Blogger call).

That said, you can create your own method that returns either a count of published posts or a list of the IDs of the published posts. Just create a quick plug-in to hook into the XML-RPC system to add your endpoint and method:

function xml_add_method( $methods ) {
    $methods['myNamespace.postCount'] = 'get_post_count';
    $methods['myNamespace.postIDList'] = 'get_post_id_list';
    return $methods;
}
add_filter( 'xmlrpc_methods', 'xml_add_method' );

That code block will add two new calls to your XML-RPC system, myNamespace.postCount and myNamespace.postIDList. You can call these remotely to return a count of published posts and a list of published post IDs, respectively.

You also need to define the PHP functions that will handle the request - all the XML-RPC system does it route remote requests to internal PHP functions that return data:

function get_post_count( $args ) {
    global $wpdb;

    ... code that retrieves the total count of published posts from the database ...

    return $count;
}

function get_post_id_list( $args ) {
    global $wpdb;

    ... code that retrieves a list of published posts from the database ...

    return $postlist;
}

That's it. Pull all the code together into a custom plug-in, place it in your site, activate it, and you can now get a count of published posts or a list of published post IDs via XML-RPC.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文