728x90
728x170

WordPress API 를 이용하여 Post 내용을 수정하는 방법입니다.

기본 : 'WordPressPCL' Nuget  패키지 설치

 

GetByIDAsync 메서드를 이용해 PostID 기준 Post 데이터를 가져오고 내용을 수정하여 UpdateAsync 메서드를 통해 Post 데이터를 수정합니다.

private async void updateButton_Click(object sender, EventArgs e)
{
    this.logTextBox.Text += $"Update Post -----------------------------------------------" + Environment.NewLine;

    string baseURL = "https://wordpress주소.kr/wp-json/";
    string userID = "사용자명";
    string applicationPassword = "응용프로그램비밀번호";

    WordPressClient client = new WordPressClient(baseURL);

    client.Auth.UseBasicAuth(userID, applicationPassword);

    try
    {
        int postID = int.Parse(this.postIdTextBox.Text);

        Post post = await client.Posts.GetByIDAsync(postID, useAuth: true);

        if (post != null)
        {
            post.Title = new Title("포스트 제목 수정");
            post.Content = new Content("포스트 내용 수정");

            Post postUpdated = await client.Posts.UpdateAsync(post);

            this.logTextBox.Text += $"ID    : {postID} Update Complete!" + Environment.NewLine;

            Post modifypost = await client.Posts.GetByIDAsync(postID, useAuth: true);

            this.logTextBox.Text += $"제목  : {modifypost.Title.Rendered}" + Environment.NewLine;
            this.logTextBox.Text += $"내용  : {modifypost.Content.Rendered}" + Environment.NewLine;
        }
        else
        {
            this.logTextBox.Text += $"ID    : {postID} Not Exist!" + Environment.NewLine;
        }
    }
    catch (Exception ex)
    {
        this.logTextBox.Text += ex.ToString() + Environment.NewLine;
    }
}

Create Post 버튼클릭으로 Post 를 만든 후 Update Post 버튼으로 Post 를 수정한 후 Post 데이터를 다시 가져와 수정된 내용을 확인합니다.

결과

728x90
그리드형
Posted by kjun
,