【问题标题】:WordPress XMLRPC - Search Post by Slug and UpdateWordPress XMLRPC - 通过 Slug 搜索帖子并更新
【发布时间】:2023-01-20 18:35:45
【问题描述】:
标签:
wordpress
wordpress-rest-api
【解决方案1】:
我最终使用了方法,这可能会帮助某人并节省时间。将以下代码粘贴到您要从中获取数据的域的 functions.php 中
add_filter('xmlrpc_methods', 'clx_xmlrpc_methods');
function clx_xmlrpc_methods($methods) {
$methods['getPostBySlug'] = 'clx_getpost';
return $methods;
}
function clx_getpost($args) {
global $wp_xmlrpc_server;
$slug = $args["slug"];
$pargs = array(
'name' => $slug,
'post_type' => 'post',
'numberposts' => 1
);
$my_posts = get_posts($pargs);
if( $my_posts ) :
return $my_posts; //echo $my_posts[0]->ID;
endif;
}
从您的 XMLRPC 代码中使用以下代码从 slug 获取 POST 数组
$args = array(
'slug' => 'your-post-slug'
);
$postArray = $wpClient->callCustomMethod( 'getPostBySlug', $args );