【问题标题】:WordPress XMLRPC - Search Post by Slug and UpdateWordPress XMLRPC - 通过 Slug 搜索帖子并更新
【发布时间】:2023-01-20 18:35:45
【问题描述】:

有没有办法通过 XMLRPC 按 slug 搜索帖子 https://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPosts

获取帖子()似乎没有使用“名称”返回..

$args = array(
   'name'   => 'my-slug',
   'number' => 1
);
    
$post = $wpClient->getPosts( $args );

请让我知道是否有解决方法,我需要通过 slug 进行搜索,然后通过 XMLRPC 远程更新这些 slugs。干杯

【问题讨论】:

    标签: 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 );    
    

    【讨论】:

      猜你喜欢
      • 2016-12-15
      • 2010-12-16
      • 2013-02-05
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 2015-02-01
      相关资源
      最近更新 更多