Adding jobs via API
-
Hello team,
as per the title, is there a way how to add jobs programmatically via API? Long time ago I managed to create my own post type and open it to API (using register_rest_field and defining the get/update functions).
Would this work with your plugin and post types? Anything to be aware of?
thanks,
Andrej
-
Hi @andrejzito
Thanks for your question! I don’t see any problems if you will use WordPress native REST API endpoints for getting posts and postmeta. Jobs are custom post type, Job Category, Job Type are custom post type taxonomies. All of them are registered by WordPress native functions so you can get them through WordPress native endpoints.
If you plan to create your own endpoint, take this into account.
Let me know if you have other questions,
Best Regards!Hi @nsinelnikov,
I did a GET on one of our jobs (https://localizationacademy.com/wp-json/wp/v2/jb-job/11860).
It returned 200 with some data, see screenshot (from Male.com).
The problem is that all the job data is not registered with the API (except for the Job type). So I can’t read/write it.
When I made my own custom type before (to represent Certificates), I registered all the custom fields used in that post type so I can get & update them.
So the question is how to register the Job Data from your jb-job post type? I assume what we see in the admin UI are just labels, but to register the fields, we may need to use something different? E.g. instead of “Company name”, it’s “company_name” or something like that.
I hope I make some sense lol, not very experienced in this area. Thanks
Hi @andrejzito
Please use this solution:
register_rest_field( 'post', 'metadata', array( 'get_callback' => function ( $data ) { return get_post_meta( $data['id'], '', '' ); }, ));
Or
register_rest_field( 'jb-job', 'metadata', array( 'get_callback' => function ( $data ) { return get_post_meta( $data['id'], '', '' ); }, ));
Ref.: https://stackoverflow.com/questions/37641689/wp-rest-api-get-posts-with-their-meta
Let me know if that works,
Best Regards!thanks, @nsinelnikov – that actually worked! Now I just need to google how to update the fields, but I should be able to do that.
Hi @andrejzito
Thanks for letting me know! It will be great if you can share your results there. Maybe some other community users need the same functionality and this information will be useful for them.
Let me know if you have other question or mark this topic as resolved,
Best Regards!Yeah, sure. I didn’t find a way how to set all the meta fields to be update-able via API. So I tried the solution I use for other custom fields, after learning how the fields from your plugin are called. And it works fine now for me.
Here’s the snippet code:
function jb_job_meta_rest() { register_rest_field('jb-job', 'jb-application-contact', array( 'get_callback' => 'rest_get_jb_application_contact', 'update_callback' => 'rest_update_jb_application_contact' )); register_rest_field('jb-job', 'jb-location-type', array( 'get_callback' => 'rest_get_jb_location_type', 'update_callback' => 'rest_update_jb_location_type' )); register_rest_field('jb-job', 'jb-location', array( 'get_callback' => 'rest_get_jb_location', 'update_callback' => 'rest_update_jb_location' )); register_rest_field('jb-job', 'jb-company-name', array( 'get_callback' => 'rest_get_jb_company_name', 'update_callback' => 'rest_update_jb_company_name' )); register_rest_field('jb-job', 'jb-company-website', array( 'get_callback' => 'rest_get_jb_company_website', 'update_callback' => 'rest_update_jb_company_website' )); } add_action('rest_api_init', 'jb_job_meta_rest'); function rest_get_jb_application_contact($post, $field_name, $request) { $cf = get_post_meta( $post['id'], 'jb-application-contact', true ); $cf = !empty($cf) ? $cf : 0; return $cf; } function rest_update_jb_application_contact( $value, $post, $field_name ) { return update_post_meta( $post->ID, 'jb-application-contact', $value ); } function rest_get_jb_location_type($post, $field_name, $request) { $cf = get_post_meta( $post['id'], 'jb-location-type', true ); $cf = !empty($cf) ? $cf : 0; return $cf; } function rest_update_jb_location_type( $value, $post, $field_name ) { return update_post_meta( $post->ID, 'jb-location-type', $value ); } function rest_get_jb_location($post, $field_name, $request) { $cf = get_post_meta( $post['id'], 'jb-location', true ); $cf = !empty($cf) ? $cf : 0; return $cf; } function rest_update_jb_location( $value, $post, $field_name ) { return update_post_meta( $post->ID, 'jb-location', $value ); } function rest_get_jb_company_name($post, $field_name, $request) { $cf = get_post_meta( $post['id'], 'jb-company-name', true ); $cf = !empty($cf) ? $cf : 0; return $cf; } function rest_update_jb_company_name( $value, $post, $field_name ) { return update_post_meta( $post->ID, 'jb-company-name', $value ); } function rest_get_jb_company_website($post, $field_name, $request) { $cf = get_post_meta( $post['id'], 'jb-company-website', true ); $cf = !empty($cf) ? $cf : 0; return $cf; } function rest_update_jb_company_website( $value, $post, $field_name ) { return update_post_meta( $post->ID, 'jb-company-website', $value ); }
I’m sure there’s a better way to implement it, but it gets the job done for me.
all good, you can close this one ??
- The topic ‘Adding jobs via API’ is closed to new replies.