3

I am following this tutorial to create custom end points to WP-API .

I am always getting this error on hitting /wp-json/custom-plugin/v2/get-all-post-ids/ on postman to test :

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

    }

I have created a custom-plugin.php file in /plugins/custom-plugin/ directory .

<?php


    if ( ! defined( 'ABSPATH' ) ) exit;

    add_action( 'rest_api_init', 'dt_register_api_hooks' );

    function dt_register_api_hooks() {    

        register_rest_route( 'custom-plugin/v2', '/get-all-post-ids/', array(
            'methods' => 'GET',
            'callback' => 'dt_get_all_post_ids',
            ) 
            );

    }


    // Return all post IDs
    function dt_get_all_post_ids() {
        if ( false === ( $all_post_ids = get_transient( 'dt_all_post_ids' ) ) ) {
            $all_post_ids = get_posts( array(
                'numberposts' => -1,
                'post_type'   => 'post',
                'fields'      => 'ids',
            ) );
            // cache for 2 hours
            set_transient( 'dt_all_post_ids', $all_post_ids, 60*60*2 );
        }

        return $all_post_ids;
    }

?>

Please guide how can i resolve this issue.

Thanks.

  • Your code should work, do you use wordpress.org/plugins/rest-api? – Jevuska Apr 15 '16 at 11:46
  • @Jevuska Yes , i am using WP REST API Version 2.0-beta13 . Can you please test it ? – Divyanshu Jimmy Apr 15 '16 at 12:00
  • 1
    Yes' I've test it and it's work. – Jevuska Apr 15 '16 at 12:01
  • @Jevuska But it is not working in my case , what should i do to test properly ? – Divyanshu Jimmy Apr 15 '16 at 12:05
  • I don't know, i just working in local ( it's work ), then I use postman (it's work too) – Jevuska Apr 15 '16 at 12:10
1

I may have the answer to this.

I had POST in my code, but I was trying to look at the URL through a browser.

Make sure you're using the method you specify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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