Skip to content

Commit 3268738

Browse files
authored
retrieve last N posts in json format (#4)
1 parent efb3810 commit 3268738

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/AiCommand.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,37 @@ private function register_tools($server, $client) {
113113
]
114114
);
115115

116+
117+
118+
// Register tool to retrieve last N posts in JSON format.
119+
$server->register_tool([
120+
'name' => 'list_posts',
121+
'description' => 'Retrieves the last N posts.',
122+
'inputSchema' => [
123+
'type' => 'object',
124+
'properties' => [
125+
'count' => [
126+
'type' => 'integer',
127+
'description' => 'The number of posts to retrieve.',
128+
],
129+
],
130+
'required' => ['count'],
131+
],
132+
'callable' => function ($params) {
133+
$query = new \WP_Query([
134+
'posts_per_page' => $params['count'],
135+
'post_status' => 'publish',
136+
]);
137+
$posts = [];
138+
while ($query->have_posts()) {
139+
$query->the_post();
140+
$posts[] = ['title' => get_the_title(), 'content' => get_the_content()];
141+
}
142+
wp_reset_postdata();
143+
return $posts;
144+
},
145+
]);
146+
116147
$server->register_tool(
117148
[
118149
'name' => 'greet',

0 commit comments

Comments
 (0)