That a nice project ! Any code you can share so we see what you try to do ?
Pascal.
Please find the code below.. I am sure it must be a very small error
add_action( 'rest_api_init', function() {
register_api_field( 'topic',
'post_parent',
array(
'get_callback' => 'slug_get_post_meta_cb',
//'update_callback' => 'slug_update_post_meta_cb',
'update_callback' => 'slug_update_spaceship2',
'schema' => null,
)
);
});
function slug_update_spaceship2( $value, $object, $field_name ) {
return update_post_meta( $object->ID, $field_name, $value);
}
function slug_get_post_meta_cb( $object, $field_name, $request ) {
return get_post_meta( $object['id'], $field_name );
}
@pathardepavan — post_parent is in the posts table rather than postmeta so instead of updating post meta, update the object in wp_posts. So your update callback function (update_topic_post_parent
) would be something like:
function update_topic_post_parent( $value, $object, $field_name ){
$update_post_parent = array(
'ID' => (int)$object->ID, // the ID of the topic you just created
'post_parent' => (int)$value ); // whatever you want your post_parent to be as an int. You could use $field_name for 'post_parent' here but I tend to whitelist
wp_update_post( $update_post_parent )
}
and you should be able to get the post_parent from the $object directly in your get callback I think.
Also if you’re using one of the latest versions of the plugin, you should use register_rest_field as register_api_field has been deprecated.
(I’m doing this without any testing, sorry, so it might be incorrect! But I think that’s somewhere close to the answer maybe 🙂 )
Certainly! In the WordPress REST API, creating a new topic and assigning it to a parent forum involves utilizing the wp/v2
endpoints for both forums and topics. To achieve this, you can follow these steps:
### 1. Get the Forum ID
Firstly, you’ll need to know the ID of the parent forum to which you want to assign the new topic. You can retrieve this ID by making a GET request to the forums endpoint, typically /wp/v2/forums
.
For example:
`http
GET /wp/v2/forums
`
### 2. Create a New Topic
Make a POST request to the topics endpoint, usually /wp/v2/topics
, providing the necessary parameters including title
, content
, and forum
to link it to the specific forum.
For example:
`http
POST /wp/v2/topics
Content-Type: application/json
{
“title”: “New Topic Title”,
“content”: “Content of the new topic”,
“forum”: <forum_id>
}
`
Replace <forum_id>
with the ID of the parent forum retrieved from step 1.
### Example Using cURL
Using cURL, the process might look something like this:
`bash
curl -X POST -H “Content-Type: application/json” -d ‘{“title”:”New Topic Title”,”content”:”Content of the new topic”,”forum”:1}’ https://yoursite.com/wp-json/wp/v2/topics
`
Replace https://yoursite.com
with your actual WordPress site URL and 1
with the ID of the desired forum.
Remember to authenticate the API request if your WordPress site requires authentication for creating new content.
This approach allows you to create a new topic and assign it to a parent forum via the WordPress REST API. Adjust the endpoint URLs and parameters as needed based on your specific WordPress configuration.