Search code examples
wordpresswordpress-rest-api

WordPress XMLRPC - Search Post by Slug and Update


Is there anyway to search posts by slug through XMLRPC https://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPosts

getPosts() doesn't seem to return using "name"..

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

Please let me know if there is a workaround for this, I need to search by slug and then update those slugs remotely via XMLRPC. cheers


Solution

  • I ended up using Methods, this may help someone and save time.. paste the following code in functions.php of the domain you are fetching data from

     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;
     }
    

    from your XMLRPC code use the following to get POST array from slug

        $args = array(
          'slug'   => 'your-post-slug'
        );
        
        $postArray = $wpClient->callCustomMethod( 'getPostBySlug', $args );