How to get, delete or publish wordpress post from powershell using Rest API

How to get, delete or publish wordpress post from powershell using Rest API?

Yes, this is possible to get/set wordpress  data using rest api. This is very simple and quick way to publish, delete or get post from anywhere.

Step 1 – installJSON Basic Authentication” and “WP REST API” by WP REST API Team , you might want to see this for interested plugins.

Step 2 – below is code to get, delete, post method. you must have to pass username and password for basic authentication.

445-2

$params = @{
    title = "test Rest API post"
    content = "test Rest API post content"
    status = 'publish'
}
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("user:pass")))
$header = @{
Authorization=("Basic {0}" -f $base64AuthInfo)
}
$params1=$params|ConvertTo-Json
Invoke-RestMethod -Method post -Uri http://khaoodara.com/wp-json/wp/v2/posts -ContentType "application/json" -Body $params1  -Headers $header -UseBasicParsing

# for deleting post you can use rest DELETE method and just add post id at the end of URI..
#Invoke-RestMethod -Method delete -Uri http://khaoodara.com/wp-json/wp/v2/posts/6705 -ContentType "application/json" -headers $header

#for get post data you can use rest GET method ..
#Invoke-RestMethod -Method get -Uri http://khaoodara.com/wp-json/wp/v2/posts -ContentType "application/json" -headers $header

Output

(Visited 1,900 times, 1 visits today)

Add a Comment

Facebook