wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
335 stars 131 forks source link

Get custom properties #338

Closed Alex-451 closed 1 year ago

Alex-451 commented 1 year ago

the /wp-json/wp/v2/posts endpoint returns the custom property jetpack_featured_media_url which contains the thumbnail for the post.

Can I somehow get that custom property?

Post for reference (jetpack_featured_media_url is below tags):

{
        "id": 53258,
        "date": "2023-01-05T04:30:59",
        "date_gmt": "2023-01-05T12:30:59",
        "guid": {
            "rendered": "https://anitrendz.net/news/?p=53258"
        },
        "modified": "2023-01-05T06:09:04",
        "modified_gmt": "2023-01-05T14:09:04",
        "slug": "goblin-slayer-season-2-receives-teaser-trailer",
        "status": "publish",
        "type": "post",
        "link": "https://anitrendz.net/news/2023/01/05/goblin-slayer-season-2-receives-teaser-trailer/",
        "title": {
            "rendered": "Goblin Slayer Season 2 Receives Teaser Trailer"
        },
        "content": {
            "rendered": "...",
            "protected": false
        },
        "excerpt": {
            "rendered": "<p>A teaser trailer for the second season of Goblin Slayer has been revealed along with details of the show&#8217;s main staff and new production studio as part of the GA FES 2023 with Danmachi 10th anniversary event. </p>\n",
            "protected": false
        },
        "author": 24,
        "featured_media": 53262,
        "comment_status": "open",
        "ping_status": "open",
        "sticky": false,
        "template": "",
        "format": "standard",
        "meta": {
            "_mi_skip_tracking": false
        },
        "categories": [
            10,
            1053,
            137
        ],
        "tags": [
            14,
            1625,
            433,
            3176,
            180
        ],
        "jetpack_featured_media_url": "https://i0.wp.com/anitrendz.net/news/wp-content/uploads/2023/01/goblinslayer_teaserpvscreenshot.png?fit=1920%2C1080&ssl=1",
        "jetpack_shortlink": "https://wp.me/p8n4QV-dR0",
        "_links": {
            "self": [
                {
                    "href": "https://anitrendz.net/news/wp-json/wp/v2/posts/53258"
                }
            ],
            "collection": [
                {
                    "href": "https://anitrendz.net/news/wp-json/wp/v2/posts"
                }
            ],
            "about": [
                {
                    "href": "https://anitrendz.net/news/wp-json/wp/v2/types/post"
                }
            ],
            "author": [
                {
                    "embeddable": true,
                    "href": "https://anitrendz.net/news/wp-json/wp/v2/users/24"
                }
            ],
            "replies": [
                {
                    "embeddable": true,
                    "href": "https://anitrendz.net/news/wp-json/wp/v2/comments?post=53258"
                }
            ],
            "version-history": [
                {
                    "count": 3,
                    "href": "https://anitrendz.net/news/wp-json/wp/v2/posts/53258/revisions"
                }
            ],
            "predecessor-version": [
                {
                    "id": 53265,
                    "href": "https://anitrendz.net/news/wp-json/wp/v2/posts/53258/revisions/53265"
                }
            ],
            "wp:featuredmedia": [
                {
                    "embeddable": true,
                    "href": "https://anitrendz.net/news/wp-json/wp/v2/media/53262"
                }
            ],
            "wp:attachment": [
                {
                    "href": "https://anitrendz.net/news/wp-json/wp/v2/media?parent=53258"
                }
            ],
            "wp:term": [
                {
                    "taxonomy": "category",
                    "embeddable": true,
                    "href": "https://anitrendz.net/news/wp-json/wp/v2/categories?post=53258"
                },
                {
                    "taxonomy": "post_tag",
                    "embeddable": true,
                    "href": "https://anitrendz.net/news/wp-json/wp/v2/tags?post=53258"
                }
            ],
            "curies": [
                {
                    "name": "wp",
                    "href": "https://api.w.org/{rel}",
                    "templated": true
                }
            ]
        }
    }
Alex-451 commented 1 year ago

Found a workaround using CustomRequests:

var posts = await _client.CustomRequest.GetAsync<IEnumerable<ArticleDto>>($"wp/v2/posts{queryBuilder.BuildQuery()}");
public class ArticleDto : Post
{
    [JsonProperty("jetpack_featured_media_url")]
    public Uri JetpackFeaturedMediaUrl { get; set; }
}