Wordpress API Get Posts by category

我是研究僧i 提交于 2019-12-24 02:06:09

问题


Is it possible to get all posts by category?

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


回答1:


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'];



回答2:


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.




回答3:


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
}


来源:https://stackoverflow.com/questions/22968236/wordpress-api-get-posts-by-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!