0

I want to create a cross-platform app using rest APIs for the website (zoetalentsolutions.com). 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:

  1. How to create REST API for the custom post type
  2. 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.

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
    Sep 29, 2020 at 9:46
  • sure @TomJNowell, I'll narrow down my question, to more specific towards wordpress Sep 29, 2020 at 9:51

1 Answer 1

0

Answering 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();
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.