How to get WordPress posts from a single category using XML-RPC

前端 未结 1 749
谎友^
谎友^ 2021-01-16 01:08

I\'m developing an Android app for a WordPress site. I\'m using XMLRPC to fetch posts from the server and list them in the app.

Since the XMLRPC client I use for And

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 01:46

    You'll create a plugin that implements a custom function as shown in Custom XML-RPC Methods in WordPress and install it on the target site.

    escape( $args );
    
        $blog_ID     = (int) $args[0];
        $username  = $args[1];
        $password   = $args[2];
        $post_type  = $args[3];
        $category = $args[4];
        $numberposts = $args[5];
        $extra = $args[6];
    
        if ( !$user = $wp_xmlrpc_server->login($username, $password) ) {
            return $wp_xmlrpc_server->error;
        }
    
        $category_int = (int) $category;
    
        if( !empty( $category_int ) ) {
            $category_call = 'cat';
        } else {
            $category_call = 'category_name';
        }
    
        $post_args = array(
            'numberposts' => $numberposts,
            'posts_per_page' => $numberposts,
            $category_call => $category,
            'post_type' => $post_type,
            'post_status' => 'any'
        );
        if( is_array( $extra ) )
            $post_args = array_merge( $post_args, $extra );
    
        $posts_list = get_posts( $post_args );
    
        if ( !$posts_list ) {
            $wp_xmlrpc_server->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
            return $wp_xmlrpc_server->error;
        } else {
            return $posts_list;
        }
    }
    

    The following is a test file in PHP (I don't know Java):

    %s
    ', print_r( $xml->params->param->value->array->data->value, true ) ); } /** * XML-RPC call */ function query_last_posts( $methodName, $url, $parameters ) { $request = xmlrpc_encode_request( $methodName, $parameters ); $ch = curl_init(); curl_setopt( $ch, CURLOPT_POSTFIELDS, $request ); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_TIMEOUT, 1 ); $results = curl_exec( $ch ); curl_close( $ch ); return $results; } $blog_id = 1; $user = 'username'; $pass = 'password'; $type = 'post'; $category = 1; // ID or category name $num_posts = 1; $args = array( $blog_id, $user, $pass, $type, $category, $num_posts, null ); $response = query_last_posts( 'b5f.getPosts', 'http://EXAMPLE.COM/xmlrpc.php', $args ); print_xml_node( $response );

    0 讨论(0)
提交回复
热议问题