I have a custom post type called "notes" and also activated an Advanced Custom Field on that post type called "page_link". I want to add/edit the value of page_link using the REST API but I could not do it. I am only able edit native fields like title and the content. In my console, after success, noteLink is there but is equals to "null".
I have html like this:
<div class="mb-3">
<label for="formInput1" class="form-label">Title</label>
<input id="formInput1" class="new-note-title form-control" placeholder="Title">
</div>
<div class="mb-3">
<label for="formTextarea1" class="form-label">Content</label>
<textarea id="formTextarea1" name="" class="new-note-body form-control" placeholder="Content"></textarea>
</div>
<div class="mb-3">
<label for="formInput2" class="form-label">Link</label>
<input id="formInput2" class="new-note-link form-control" placeholder="Link">
</div>
javascript:
createNote(e) {
var ourNewPost = {
'title': $(".new-note-title").val(),
'noteLink': $(".new-note-link").val(),
'content': $(".new-note-body").val(),
'status': 'publish'
}
$.ajax({
beforeSend: xhr => {
xhr.setRequestHeader("X-WP-Nonce", myData.nonce)
},
url: myData.root_url + "/wp-json/wp/v2/note/",
type: "POST",
data: ourNewPost,
success: response => {
//location.reload()
console.log("Congrats")
console.log(response)
},
error: response => {
console.log("Sorry")
console.log(response)
}
})
}
register post type like this:
function custom_post_types() {
register_post_type('note', array(
'capability_type' => 'note',
'map_meta_cap' => true,
'show_in_rest' => true,
'supports' => array('title', 'editor', 'advanced-custom-fields'),
'public' => false,
'show_ui' => true,
'labels' => array(
'name' => 'Notes',
'add_new_item' => 'Add New Note',
'edit_item' => 'Edit Note',
'all_items' => 'All Notes',
'singular_name' => 'Note'
),
'menu_icon' => 'dashicons-welcome-write-blog'
));
}
register_rest_field on my functions.php like this:
function custom_rest(){
register_rest_field('note', 'noteLink', array(
'get_callback' => function(){return get_field('page_link');}
));
}
add_action("rest_api_init", 'custom_rest');