admin管理员组文章数量:1318569
I want to create a cross-platform app using rest APIs for the website (zoetalentsolutions). The website is in WordPress and has custom post-types, fields & custom queries.
This app will not have any user auth.
What I want is to get custom post-type (in my case: courses) in REST & need to run a custom query (custom meta-query).
So I need a little guidance:
- How to create REST API for the custom post type
- How to create end-point for a custom query.
Any help would be appreciated around this. I'm not expecting a thorough and but a details steps with brief would be helpful. I believe the second question needs to be asked as a separate but I only need a brief idea of it.
Thank you.
I want to create a cross-platform app using rest APIs for the website (zoetalentsolutions). The website is in WordPress and has custom post-types, fields & custom queries.
This app will not have any user auth.
What I want is to get custom post-type (in my case: courses) in REST & need to run a custom query (custom meta-query).
So I need a little guidance:
- How to create REST API for the custom post type
- How to create end-point for a custom query.
Any help would be appreciated around this. I'm not expecting a thorough and but a details steps with brief would be helpful. I believe the second question needs to be asked as a separate but I only need a brief idea of it.
Thank you.
Share Improve this question edited Sep 29, 2020 at 9:56 Danish Mohammed asked Sep 29, 2020 at 9:27 Danish MohammedDanish Mohammed 135 bronze badges 2- Can you narrow your question down to a single specific question that can be answered with facts? This is a QA site rather than a discussion forum, so you need to be able to mark an answer as the factually correct answer, not just helpful advice. Keep in mind that the stack is focused on WordPress development rather than pure JS, so react native vs flutter isn't within the scope of this site, neither are JS library recommendations – Tom J Nowell ♦ Commented Sep 29, 2020 at 9:46
- sure @TomJNowell, I'll narrow down my question, to more specific towards wordpress – Danish Mohammed Commented Sep 29, 2020 at 9:51
1 Answer
Reset to default 0Answering questions myself. I hope this will help some folks.
1.How to create REST API for the custom post type
If you're creating a new custom post type then make sure this option is set to true in your arguments array:
show_in_rest' => true,
If your custom post is from a theme or plugin and you want to enable rest-api for it, then add this code into your child-theme functions.php
/**
* Add REST API support to an already registered post type.
*/
add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 );
function my_post_type_args( $args, $post_type ) {
if ( 'course' === $post_type ) {
$args['show_in_rest'] = true;
// Optionally customize the rest_base or rest_controller_class
$args['rest_base'] = 'course';
$args['rest_controller_class'] = 'WP_REST_Posts_Controller';
}
return $args;
}
2.How to create an end-point for a custom query.
Let's say you want to run some wp-query and needs to send a custom parameter & access that query from rest-API
add_action('rest_api_init', function () {
register_rest_route( 'namespace/v2', 'events/(?P<id>\d+)',array(
'methods' => 'GET',
'callback' => 'get_events_from_id'
));
});
//Now create a function to return your custom query results
function get_events_from_id($request){
$id = $request['id'];
// Custom WP query query
$args_query = array(
'post_type' => array('events'),
'order' => 'DESC',
);
$query = new WP_Query( $args_query );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$data = //do your stuff like
}
} else {
}
return $data;
wp_reset_postdata();
}
本文标签: customizationCreate API39s for custompost types amp custom queries using REST or Graphql
版权声明:本文标题:customization - Create API's for custom-post types & custom queries using REST or Graphql 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742047198a2417858.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论