docs: investigate and document custom fields (meta) support#426
docs: investigate and document custom fields (meta) support#426
Conversation
…mples Agent-Logs-Url: https://github.com/wp-net/WordPressPCL/sessions/c7a1e61b-f0aa-4c3c-9f9b-2101f1256f8c Co-authored-by: ThomasPe <4225039+ThomasPe@users.noreply.github.com>
Agent-Logs-Url: https://github.com/wp-net/WordPressPCL/sessions/c7a1e61b-f0aa-4c3c-9f9b-2101f1256f8c Co-authored-by: ThomasPe <4225039+ThomasPe@users.noreply.github.com>
|
@copilot are there sufficien integration tests for the custom fields support? |
… tests Agent-Logs-Url: https://github.com/wp-net/WordPressPCL/sessions/16d8672f-5eb5-4463-aaa2-0935f2f6d35b Co-authored-by: ThomasPe <4225039+ThomasPe@users.noreply.github.com>
Added two integration tests in commit e9e33b5:
The test environment is prepared via a mu-plugin written by |
There was a problem hiding this comment.
Pull request overview
Adds/updates documentation and self-hosted integration tests to clarify and validate WordPress custom fields (“meta”) usage in WordPressPCL, aligning code examples with the existing JsonElement? Meta model shape.
Changes:
- Added a dedicated v3 documentation page describing opt-in REST meta exposure, reading/writing patterns, and CustomRequest usage for top-level fields.
- Fixed v2/v3 posts docs examples to use
JsonSerializer.SerializeToElement(...)forMeta(instead of a non-compilingDictionary<string,string>assignment) and linked to the new custom fields doc. - Added self-hosted integration tests for writing and updating registered post meta; updated
.gitignoreto correctly ignoresite/.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
tests/WordPressPCL.Tests.Selfhosted/Posts_Tests.cs |
Adds two integration tests covering post meta write/read and meta-only updates. |
dev/init-wordpress.sh |
Registers a REST-exposed test meta key via a mu-plugin for repeatable integration tests. |
docs/v3/entities/customFields.md |
New reference doc for custom fields/meta, including top-level field guidance via CustomRequest. |
docs/v3/entities/posts.md |
Fixes Meta update example to use JsonElement serialization; links to custom fields doc. |
docs/v2/entities/posts.md |
Fixes Meta update example to use JsonElement serialization. |
mkdocs.yml |
Adds the new “Custom Fields” page to v3 navigation. |
.gitignore |
Splits site/ onto its own line so it’s actually ignored. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Act — create then fetch back (edit context to ensure meta is returned) | ||
| Post createdPost = await _clientAuth.Posts.CreateAsync(post, TestContext.CancellationToken); | ||
| Post fetchedPost = await _clientAuth.Posts.GetByIdAsync(createdPost.Id, cancellationToken: TestContext.CancellationToken); | ||
|
|
There was a problem hiding this comment.
GetByIdAsync defaults to useAuth: false and also doesn’t let you request context=edit. Since meta fields are often only returned in edit context for authenticated requests, this test can intermittently read an empty/omitted Meta even though the write succeeded. Consider fetching the created post via Posts.QueryAsync with new PostsQueryBuilder { Include = [createdPost.Id], Context = Context.Edit } and useAuth: true (or use CustomRequest against wp/v2/posts/{id}?context=edit with useAuth: true).
| Assert.IsNotNull(fetchedPost.Meta, "Meta should not be null when a registered key was written"); | ||
| string? readValue = fetchedPost.Meta.Value.GetProperty("wordpresspcl_test_meta").GetString(); | ||
| Assert.AreEqual(metaValue, readValue, "Meta value read back should equal the value written"); |
There was a problem hiding this comment.
fetchedPost.Meta.Value.GetProperty("wordpresspcl_test_meta") will throw if WordPress returns an empty {} (or the key is absent) even when Meta itself is non-null. To make failures clearer and avoid an unhandled KeyNotFoundException, use TryGetProperty and assert the property exists before reading the string value.
| await _clientAuth.Posts.UpdateAsync(updateRequest, TestContext.CancellationToken); | ||
|
|
||
| // Assert — fetch back and verify | ||
| Post fetchedPost = await _clientAuth.Posts.GetByIdAsync(createdPost.Id, cancellationToken: TestContext.CancellationToken); |
There was a problem hiding this comment.
Same issue as above: the verification fetch uses GetByIdAsync without useAuth: true and can’t set context=edit, which may prevent the updated meta from being returned. Prefer querying by Include with Context = Context.Edit and useAuth: true (or a CustomRequest to ...?context=edit).
| Post fetchedPost = await _clientAuth.Posts.GetByIdAsync(createdPost.Id, cancellationToken: TestContext.CancellationToken); | |
| List<Post> fetchedPosts = (await _clientAuth.Posts.QueryAsync(new PostsQueryBuilder | |
| { | |
| Include = new List<int> { createdPost.Id }, | |
| Context = Context.Edit | |
| }, useAuth: true, cancellationToken: TestContext.CancellationToken)).ToList(); | |
| Post fetchedPost = fetchedPosts.Single(); |
| | `PostRevision` | `/wp/v2/revisions` | | ||
| | `User` | `/wp/v2/users` | | ||
|
|
There was a problem hiding this comment.
The table lists the PostRevision endpoint as /wp/v2/revisions, but this library’s revisions client routes under the parent post (e.g. wp/v2/posts/{postId}/revisions). Please update the endpoint column (and consider adding a short note that revisions are nested under posts).
| | `PostRevision` | `/wp/v2/revisions` | | |
| | `User` | `/wp/v2/users` | | |
| | `PostRevision` | `/wp/v2/posts/{postId}/revisions` | | |
| | `User` | `/wp/v2/users` | | |
| > **Note:** Revisions are nested under their parent post, so the revisions route includes the post ID. |
| | `Comment` | `/wp/v2/comments` | | ||
| | `MediaItem` | `/wp/v2/media` | | ||
| | `PostRevision` | `/wp/v2/revisions` | | ||
| | `User` | `/wp/v2/users` | |
There was a problem hiding this comment.
This section claims the Meta property is available only on the listed models, but the codebase also exposes Meta on term models like Category and Tag. Either expand the list to include those models/endpoints (term meta) or clarify that the list is limited to the primary entities covered by this doc.
| | `User` | `/wp/v2/users` | | |
| | `User` | `/wp/v2/users` | | |
| | `Category` | `/wp/v2/categories` | | |
| | `Tag` | `/wp/v2/tags` | |
WordPress exposes custom fields via an opt-in
metaproperty on posts, pages, comments, users, and media. The library already models this asJsonElement?on all relevant types; what was missing was documentation, correct code examples, and integration tests.Changes
New
docs/v3/entities/customFields.md— dedicated custom fields reference covering:metais empty by default and how to opt in server-side (register_post_meta,register_rest_field, ACF)Post,Page,Comment,MediaItem,PostRevision,User)JsonElement?(raw access + strongly-typed deserialization)JsonSerializer.SerializeToElement()acfkey) viaCustomRequestmkdocs.yml— registered the new page in v3 navigationBug fix in
docs/v2/entities/posts.mdanddocs/v3/entities/posts.md— both hadMeta = new Dictionary<string, string> { ... }which doesn't compile;MetaisJsonElement?. Corrected to:.gitignorefix —site/was concatenated onto a previous line, making it ineffective; split to its own lineIntegration tests — two new self-hosted integration tests in
tests/WordPressPCL.Tests.Selfhosted/Posts_Tests.cs:Posts_Meta_Write_And_Read— creates a post with a registered meta key set to a unique value and asserts the value is returned on the next fetchPosts_Meta_Update— creates a post, updates only its meta field via a partialPostobject, and verifies the new value is reflected on the next fetchThe self-hosted test environment is prepared in
dev/init-wordpress.shby writing a mu-plugin that callsregister_post_meta('post', 'wordpresspcl_test_meta', ['show_in_rest' => true, ...]), making the key available through the REST API for all test runs.