How to get WordPress posts from a single category

2019-02-20 02:18发布

问题:

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 Android enables me to call the WordPress' methods and handle the result, I'm completely dependent on the methods available in WordPress' class-wp-xmlrpc-server.php file.

So far, I used wp.getPosts() method from the WordPress XMLRPC API, and I successfully fetched the most recent posts, like so:

Object[] params = {1, username, password}; // parameters for wp.getPosts(), blogID (1), username and password.

XMLRPCClient client = new XMLRPCClient(url, username, password);

Object[] posts = client.call("wp.getPosts", params);

The problem: I want to be able to get recent posts but from ONE category only, not from the entire website (all categories) like it did above. I've searched through [WordPress XMLRPC API documentation][3] and I couldn't find any parameter that I could pass to wp.getPosts() that would allow me to retrieve posts only from one specific category.

I feel like there is a simple solution to this, but I seem to miss it.

回答1:

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.

<?php
/**
 * Plugin Name: (SO) Custom XML-RPC
 * Author: brasofilo
 * Plugin URI: http://stackoverflow.com/a/25507463/1287812
 */

add_filter( 'xmlrpc_methods', 'add_my_xmlrpc_methods' );

function add_my_xmlrpc_methods( $methods ) {
    $methods['b5f.getPosts'] = 'b5f_xmlrpc_get_posts';
    return $methods;
}

function b5f_xmlrpc_get_posts( $args ) {
    global $wp_xmlrpc_server;
    $wp_xmlrpc_server->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):

<?php
/**
 * Auxiliary function: prints the main node from the returned value
 */
function print_xml_node( $string )
{
    $xml = simplexml_load_string( $string );
    printf( '<pre><code>%s</code></pre>', 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 );