0

I am trying to update a specific meta_key field using an API. meta_key name is _yoast_seo_redirect. it contains a redirect URL which we give in post options within wordpress.

What I am trying to achieve is, whenever someone hit the url like :

http://yoursite.com/wp-json/post_info/post?id=59&meta_value=www.google.com

it should update the post_id : 59 with meta_value : www.google.com

Write now, I am getting json error while trying to achieve this :

[
{
code: "json_no_route",
message: "No route was found matching the URL and request method"
}
]

Here is my function using which I am trying to achieve :

function get_post_details ($params)
{
    $id = $_GET['id'];
    $meta_value = $_GET['meta_value'];  

    if(update_post_meta( $id, '_yoast_wpseo_redirect', $meta_value ))
    {
        $post['status'] = 'Successfully Updated';
        $post['flag'] = '1';
    }
    else
    {
        $post['status'] = 'Error';
        $post['flag'] = '0';    
    }

    if( empty( $post ) ){
        return null;
    }

    return $post;
}

 // Register the rest route here.

add_action( 'rest_api_init', function () {
    register_rest_route( 'post_info', 'post',array(

            'methods'  => 'GET',
            'callback' => 'get_post_details'

        ));

});

Any help would be appreciated. Thank You !

  • And where exactly is your code failing you? – kero Jul 23 '18 at 8:09
  • @kero I have updated the question with error I am getting. Thank You – Ankit Pandey Jul 23 '18 at 8:10
1

I can't really recreate the error. Are you sure the script is properly loaded?

function get_post_details ($params)
{
    return rest_ensure_response( 'Hello World, this is the WordPress REST API' );
}

// Register the rest route here.
add_action( 'rest_api_init', function () {
    register_rest_route( 'post_info', 'post',array(
            'methods'  => 'GET',
            'callback' => 'get_post_details'
        ));
});

This gave me the expected response - so the route was correctly registered at

http://www.example.com/wp-json/post_info/post

As for the arguments, as suggested in the REST API docs, you should use $params instead of $_GET. Changing this and adding $post = array(); (so the variable is properly declared), made your code work on my local machine. You should probably check first, if these values are even set, also

function get_post_details ($params)
{
    $id = $params['id'];
    $meta_value = $params['meta_value'];

    $post = array();

    if(update_post_meta( $id, '_yoast_wpseo_redirect', $meta_value ))
    {
        $post['status'] = 'Successfully Updated';
        $post['flag'] = '1';
    }
    else
    {
        $post['status'] = 'Error';
        $post['flag'] = '0';
    }

    if( empty( $post ) ){
        return null;
    }

    return $post;
}

// Register the rest route here.
add_action( 'rest_api_init', function () {
    register_rest_route( 'post_info', 'post',array(
            'methods'  => 'GET',
            'callback' => 'get_post_details'
        ));
});

and the route

http://www.example.com/wp-json/post_info/post?id=21&meta_value=https://www.google.com

If you don't have pretty permalinks enabled, the route may look different!

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.