heymega
heymega

Reputation: 9401

Wordpress API Get Posts by category

Is it possible to get all posts by category?

http://codex.wordpress.org/XML-RPC_WordPress_API/Posts

Upvotes: 2

Views: 2000

Answers (3)

heymega
heymega

Reputation: 9401

I managed to get this working in the end by using the optional filters parameter. According to the documentation these are the following accepted filter parameters.

struct filter: Optional. string post_type string post_status int number int offset string orderby string order

Out of curiousity I sent over a 'category' filter by adding the following to class-wp-xmlrpc-server.php under the getposts method

if(isset($filter['category']))
     $query['category'] = absint($filter['category']);

before the wp_get_recent_posts{$query); method is called

and it worked! It seems like Wordpress have left out a few filter parameters from their documentation.

You can also send over a search filter by passing over 's' as the filter

$query['s'] = $filter['s'];

Upvotes: 3

brasofilo
brasofilo

Reputation: 26065

It's quite easy after all. From this WPSE answer, we learn that we can extend the XML-RPC methods and create our own my.getPosts, like show by this William P. Davis' extension code on GitHub - - fork.

Basically, create a plugin with the following code and adapt the arguments accepted to produce the output:

add_filter( 'xmlrpc_methods', 'add_my_xmlrpc_methods' );

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

function bdn_xmlrpc_get_posts( $args ) {
    # Adapt wp.getPosts to your needs
    # https://core.trac.wordpress.org/browser/tags/3.9/src/wp-includes/class-wp-xmlrpc-server.php#L1553
}

Upvotes: 1

Kyle
Kyle

Reputation: 735

It doesn't look like there is a way to filter beforehand. You may need to run the API call and filter afterwards.

You'd probably be best off asking on the stackexchange wordpress site though.

Upvotes: 1

Related Questions