Hello!
In file 'xmlrpc.php' we can find out useful functions.
Also here: link
What I want to do is to obtain (complete) list of post ID's for given category Name or ID i.e. getPosts(4)
I have found two functions in xmlrmpc.php which we can use:
a) mw_getCategories and b) mw_getRecentPosts
For a) myBlog.getCategories() (I'm using C#)
We receive struct array of Categories which contains 'Category Id' , 'Name' etc.
1st question. How I can add to those structs, array of posts id's which are in this category ?
(I can get post giving the post id, that's why I want to obtain posts id's of specific category)
For b) myBlog.getRecentPosts(20) where 20 is the numer of posts to receive
2nd question. How I can change this function to give category name/id and return all posts in that category id/name ?
Any ideas? Could someone help me with this?
Best Regards,
Matt
Example in files:
function mw_getCategories($args) {
$this->escape($args);
$blog_ID = (int) $args[0];
$username = $args[1];
$password = $args[2];
if ( !$user = $this->login($username, $password) ) {
return $this->error;
}
if( !current_user_can( 'edit_posts' ) )
return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this blog in order to view categories.' ) );
do_action('xmlrpc_call', 'metaWeblog.getCategories');
$categories_struct = array();
if ( $cats = get_categories('get=all') ) {
foreach ( $cats as $cat ) {
$struct['categoryId'] = $cat->term_id;
$struct['parentId'] = $cat->parent;
add here i.e $struct['AllPosts'] which will be contains all posts ID's in this category.
$struct['description'] = $cat->name;
$struct['categoryDescription'] = $cat->description;
$struct['categoryName'] = $cat->name;
$struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
$struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
$categories_struct[] = $struct;
}
}
return $categories_struct;
}
function mw_getRecentPosts($args) {
$this->escape($args);
$blog_ID = (int) $args[0];
$username = $args[1];
$password = $args[2];
$num_posts = (int) $args[3];
if ( !$user = $this->login($username, $password) ) {
return $this->error;
}
do_action('xmlrpc_call', 'metaWeblog.getRecentPosts');
$posts_list = wp_get_recent_posts($num_posts);
change num_posts to category_id and recive all posts in that category
if (!$posts_list) {
return array( );
}
foreach ($posts_list as $entry) {
if( !current_user_can( 'edit_post', $entry['ID'] ) )
continue;
$post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);
$post_date_gmt = mysql2date('Ymd\TH:i:s', $entry['post_date_gmt'], false);
// For drafts use the GMT version of the date
if ( $entry['post_status'] == 'draft' ) {
$post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $entry['post_date'] ), 'Ymd\TH:i:s' );
}
$categories = array();
$catids = wp_get_post_categories($entry['ID']);
foreach($catids as $catid) {
$categories[] = get_cat_name($catid);
}
$tagnames = array();
$tags = wp_get_post_tags( $entry['ID'] );
if ( !empty( $tags ) ) {
foreach ( $tags as $tag ) {
$tagnames[] = $tag->name;
}
$tagnames = implode( ', ', $tagnames );
} else {
$tagnames = '';
}
$post = get_extended($entry['post_content']);
$link = post_permalink($entry['ID']);
// Get the post author info.
$author = get_userdata($entry['post_author']);
$allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
$allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
// Consider future posts as published
if( $entry['post_status'] === 'future' ) {
$entry['post_status'] = 'publish';
}
$struct[] = array(
'dateCreated' => new IXR_Date($post_date),
'userid' => $entry['post_author'],
'postid' => $entry['ID'],
'description' => $post['main'],
'title' => $entry['post_title'],
'link' => $link,
'permaLink' => $link,
// commented out because no other tool seems to use this
// 'content' => $entry['post_content'],
'categories' => $categories,
'mt_excerpt' => $entry['post_excerpt'],
'mt_text_more' => $post['extended'],
'mt_allow_comments' => $allow_comments,
'mt_allow_pings' => $allow_pings,
'mt_keywords' => $tagnames,
'wp_slug' => $entry['post_name'],
'wp_password' => $entry['post_password'],
'wp_author_id' => $author->ID,
'wp_author_display_name' => $author->display_name,
'date_created_gmt' => new IXR_Date($post_date_gmt),
'post_status' => $entry['post_status'],
'custom_fields' => $this->get_custom_fields($entry['ID'])
);
}
$recent_posts = array();
for ($j=0; $j<count($struct); $j++) {
array_push($recent_posts, $struct[$j]);
}
return $recent_posts;
}