0
votes

{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}

Im trying to determine whats wrong with my code. Ive written a custom api endpoint for wordpress but I get the above error while trying to access the url. My code is as follows:

 $this->loader->add_action('rest_api_init', $plugin_public, 'create_api_webhook');

 public function create_api_webhook() {
    register_rest_route('learn2/v1', '/api/', array(
        'methods' => 'GET',
        'callback' => array($this, 'learn2_api_webhook')
    ));
}

public function learn2_api_webhook() {
    global $wpdb;
    $args = array('post_type' => 'sfwd-courses',
        'post_status' => 'publish',
        'order' => 'DESC',
        'orderby' => 'ID',
    );
    $loop = new WP_Query($args);
    $courses = array();
    $lessons = array();
    while ($loop->have_posts()) : $loop->the_post();

        $mylessons = learndash_get_lesson_list($post->ID);
        $lessons = array();
        foreach ($mylessons as $lesson) {
            $topics = array();
            $mytopics = learndash_get_topic_list($lesson->ID, $post->ID);
            foreach ($mytopics as $topic) {
                $topics[] = array('id' => $topic->ID, 'title' => $topic->post_title);
            }
            $lessons[] = array('id' => $lesson->ID, 'title' => $lesson->post_title, 'topics' => $topics);
        }
        $courses[] = array('id' => $post->ID, 'title' => $post->post_title, 'lessons' => $lessons);


    endwhile;


    return json_encode($courses);
}

Im trying to access the url with:

http://localhost/community_staging/wp-json/learn2/v1/api/ but I get the above error.

Im running the latest wordpress

1

1 Answers

0
votes
$this->loader->add_action('rest_api_init', $plugin_public, 'create_api_webhook');

$plugin_public wasn't pointing to the right class.