Create, Update Or Delete WordPress Posts / Custom Post Type Using REST API
There is three-stage of first we need to create a post with a simple one filed Title, Then we need to update the post and delete the post. The same process will work for Post type and Custom post type.
Where to insert all the code from this post? I hope you know this but just in case.
You need a postman and Website through Postman we are going to insert value in post type.
We are going to use wp_remote_post() it is a WordPress function it should be inserted somewhere in the WordPress environment.
$api_response = wp_remote_post( 'https://domainname/wp-json/wp/v2/posts', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'LOGIN:PASSWORD' )
),
'body' => array(
'title' => 'My test',
'status' => 'draft', // ok, we do not want to publish it immediately
'content' => 'lalala',
'categories' => 5, // category ID
'tags' => '1,4,23' // string, comma separated
'date' => '2015-05-05T10:00:00', // YYYY-MM-DDTHH:MM:SS
'excerpt' => 'Read this awesome post',
'password' => '12$45',
'slug' => 'new-test-post' // part of the URL usually
// more body params are here:
// developer.wordpress.org/rest-api/reference/posts/#create-a-post
)
) );
$body = json_decode( $api_response['body'] );
// you can always print_r to look what is inside
// print_r( $body ); // or print_r( $api_response );
if( wp_remote_retrieve_response_message( $api_response ) === 'Created' ) {
echo 'The post ' . $body->title->rendered . ' has been created successfully';
}
Update a Post type with REST API
We are going update the title of the created post. Replace {POST_ID} with the ID of the post you would like to update. check the below sample
$api_response = wp_remote_post( 'https://domainname/wp-json/wp/v2/posts/{POST_ID}/', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'LOGIN:PASSWORD' )
),
'body' => array(
'title' => 'My test 1'
)
) );
$body = json_decode( $api_response['body'] );
if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
echo 'The post ' . $body->title->rendered . ' has been updated successfully';
}
If you add ?force=true
at the end of the request URI, the post will be removed permanently without moving to trash.
$api_response = wp_remote_request( 'https://domainname/wp-json/wp/v2/posts/{POST_ID}', array( // ?force=true to skip trash
'method' => 'DELETE',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'LOGIN:PASSWORD' )
)
));
$body = json_decode( $api_response['body'] );
if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
if( $body->deleted == true ) {
echo 'The post ' . $body->previous->title->rendered . ' has been completely deleted';
} else {
echo 'The post ' . $body->title->rendered . ' has been moved to trash';
}
}
If you don't required Loing just remove Loing functionality from the function and customize based on your requirement.
Your Business Website is the most essential part of a large scale marketing base for your newly laun
At the point when the world is incapacitated in the wake of persevering through a pandemic of an ext
After search engines, job portals are almost certainly the most highly visited sites in the internet
In this article, we are going to discuss web design ideas for business. Before planing to develop th