• Resolved Jason Judge

    (@judgej)


    My requirement is to get the titles of jobs through an API, for an external application. Other details will come later, but just the titles – listings and by job ID – is all I need at this stage.

    So, I see there is work underway in github to incorporate the WP REST API into WP Job Manager. There are plenty of commits, but no real discussion and no examples of how it will work, so far as I can see.

    My question is, is any of the API usable at this time? Is there an estimated timeframe for any releases of the API? For now, would I need to extend the WP REST API myself with a custom endpoint just to get at the job titles?

    • This topic was modified 7 years, 8 months ago by Jason Judge.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Jason Judge

    (@judgej)

    This snippet in functions.php does the trick for now, until I know what is happening with the API officially:

    /**
     * API Access to job titles and some other limited data.
     * This will go into a plugin of its own at some point.
     */
    add_action('rest_api_init', function () {
        register_rest_route( 'wpjm_public/v1', '/title/(?P<id>\d+)',[
            'methods' => 'GET',
            'callback' => function(WP_REST_Request $request) {
                // The ID of the job we want.
                $post_id = $request->get_param('id');
                // Get the job.
                $post = get_post($post_id);
                // Return the limited job details, if it is a job listing.
                if ($post->post_type == 'job_listing') {
                    return [$post_id => ['post_title' => $post->post_title]];
                }
                // No post or the wrong type of post.
                return new WP_Error('invalid_job', 'Invalid job', ['status' => 404]);
            },
        ]);
    });

    I can get a job title using a URL like this:

    https://example.com/wp-json/wpjm_public/v1/title/41820

    The result is an array, and the job title is in any array, to allow for extending to return multiple jobs and multiple data items per job. It is limited by design as it is not taking into account any group security on the job listings, so only the minimum is exposed.

    • This reply was modified 7 years, 8 months ago by Jason Judge.
    • This reply was modified 7 years, 8 months ago by Jason Judge.
    Plugin Contributor jonryan

    (@jonryan)

    @judgej thanks for sharing that snippet. We don’t really have a timeline right now when the API will be done, however the intention is to allow WPJM to be completely API driven.

    Thread Starter Jason Judge

    (@judgej)

    Thanks. I’ve since added get_permalink() and a few other bits of public data to my snippet. There is no security enforced on it, as none is needed in this case. Hopefully it will be useful to someone in the interim.

    I will watch and wait with eager anticipation. It’s all good ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘What API facilities are available?’ is closed to new replies.