3

I am writing an Angular WP theme and i'm trying to reduce the number of HTTP requests on the post page.

On the post page I want to list all the different taxonomies, recent posts, get the featured image and a few other things. I can do this all with individual requests with the REST API v2 plugin but that's a lot of requests.

I was hoping to create an endpoint for my theme, parse the post slug and get it all back in one request but I can't seem to figure it out.

I was thinking of using query string to get the slug. Here's what I have been using to test it out:

function app_get_post($data) {
    global $wp_query;

    return [
        'test' => $data,
        'vars' => $wp_query->query_vars
    ];
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'app/v1', '/post', [
        'methods' => 'GET',
        'callback' => 'app_get_post',
    ] );
} );

Here's what it produces:

{
test: { },
vars: [ ]
}

I did try adding the query var with a query_vars hook but it didn't work either.

Any suggestions? Am I going about this the right way?

3 Answers 3

3

you should pass the parameter

function app_get_post($data) {

    return [
        'test' => $data["postid"]        
    ];
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'app/v1', '/post/(?P<postid>\d+)', [
        'methods' => 'GET',
        'callback' => 'app_get_post',
    ] );
} )

refer http://wiki.workassis.com/wordpress-create-rest-api/ for example

Sign up to request clarification or add additional context in comments.

Comments

0

So I'm learning about all this as well and haven't met success yet but will offer my research points. I know that to tap into a Custom Post Type you would need to add show_in_rest=true to the register_post_type() array or hook into it later.

However your example shows the use of post which I'm guessing should have show_in_rest where it is registered... I decided to check in wp-includes/post.php and it does not exist.

// ACCESSING A CPT FROM ANYWHERE (unable to add show_in_rest=true to the register_post_type() hook into it!)
// http://scottbolinger.com/custom-post-types-wp-api-v2/
function sb_add_cpts_to_api( $args, $post_type ) {
    if ( 'movie' === $post_type ) {
        $args['show_in_rest'] = true;
        $args['rest_base'] = 'movie';
        // $args['rest_controller_class'] = 'WP_REST_Posts_Controller';
    }
    return $args;
}
add_filter( 'register_post_type_args', 'sb_add_cpts_to_api', 10, 2 );

Comments

0

Here you need to pass Parameter as show below

//Endpoint : http://wpdadd.com/wp-json/api/v1/getwp/{parameter 1}/{parameter2}

register_rest_route( 'api', '/v1/getwp/(?P<param1>[a-z0-9\-]+)/(?P<param2>[a-z0-9\-]+)', array(
    'methods' => 'GET',
    'callback' => 'wpdaddStuff'

));

for more information you can check this example here http://wpdadd.com/create-custom-rest-api-wordpress/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.