• Resolved James Morton

    (@stylishjm)


    Hi,

    I’ve got a Powershell script set up to create a new post in WordPress (which works just fine) using WP REST API (2.0-beta13.1). At the same time as creating posts, I’d like to get custom fields populated with data.

    Below is an excerpt of the test script

    $content = @{
                title='status-123456789'
                status='publish'
                content='test'
                categories=@('22')
              }
    $json = $content | ConvertTo-Json
    Invoke-RestMethod -Uri $uri -Method Post -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType "application/json" -Body $json 

    As mentioned, this posts fine to my dev WordPress install.

    I’m pretty new to all things JSON/RESTful so would appreciate assistance on populating the ACF fields when creating a new post.

    So far, I have read that to populate ACF, I need to use a different endpoint – would I then essentially need to make two requests, first to post using the standard endpoint, then to get the post ID from the response and update that ID with the ACF endpoint? Or can I do it in one request?

    Again, very new to this so as much detail as possible would be great.

    Thanks

    • This topic was modified 8 years, 5 months ago by James Morton.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter James Morton

    (@stylishjm)

    So to follow up on this, I’ve decided to try the route of sending over a normal post request, then to retrieve the post ID from the response and use it within the second response, but so far not getting very far – currently a Internal Server Error 500 gets returned

    $uri1 = "https://xxx.com/wp-json/wp/v2/posts/"
    $content1 = @{
                title='status-2'
                status='publish'
                content='test'
                categories=@('22')
              }
    $json1 = $content1 | ConvertTo-Json 
    Invoke-RestMethod -Uri $uri1 -Method Post -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType "application/json" -Body $json1 | select -expand id -outvariable postid | out-null
    
    $uri2 = "https://xxx.com/wp-json/acf/v2/post/$postid"
    $content2 = @{
                 site_id = 'status-2'
              }
    $json2 = $content2 | ConvertTo-Json 
    Invoke-RestMethod -Uri $uri2 -Method Post -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType "application/json" -Body $json2 
    
    • This reply was modified 8 years, 5 months ago by James Morton.
    Thread Starter James Morton

    (@stylishjm)

    Figured it out, need to add this to functions.php

    	add_filter( 'acf/rest_api/key', function( $key, $request, $type ) {
    		return 'acf_fields';
    	}, 10, 3 );

    ..and then adjust my powershell code accordingly

        $content2 = @{
                        acf_fields=@{
                            site_id="$siteid"
                            primary_online="$status1"
                            primary_active="$status2"
                            backup_online="$status3"
                            backup_active="$status4"
                            ip="$primary"
                            date_time="$datetime2"
                            brand="$brand"
                        }
                  }
        $json2 = $content2 | ConvertTo-Json 
        Invoke-RestMethod -Uri $uri2 -Method Post -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType "application/json" -Body $json2
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Create new Post and populate ACF fields’ is closed to new replies.