From 6b72de2977c227307412bae09c1be6bb41f464b6 Mon Sep 17 00:00:00 2001 From: Thor Brink Date: Tue, 14 Nov 2023 09:04:40 +0100 Subject: [PATCH] fix: add api posts to cache to enable get_post (#759) --- .../ResourceFromApi/PostType/PostTypeQueriesModifier.php | 2 +- .../ResourceFromApi/PostType/PostTypeResourceRequest.php | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/library/Content/ResourceFromApi/PostType/PostTypeQueriesModifier.php b/library/Content/ResourceFromApi/PostType/PostTypeQueriesModifier.php index 1194d283b..dd7ce861c 100644 --- a/library/Content/ResourceFromApi/PostType/PostTypeQueriesModifier.php +++ b/library/Content/ResourceFromApi/PostType/PostTypeQueriesModifier.php @@ -26,7 +26,7 @@ public function addHooks(): void add_filter('Municipio/Breadcrumbs/Items', [$this, 'modifyBreadcrumbsItems'], 10, 3); add_action('pre_get_posts', [$this, 'preventSuppressFiltersOnWpQuery'], 200, 1); add_action('pre_get_posts', [$this, 'preventCacheOnPreGetPosts'], 200, 1); - add_action('Municipio/Content/ResourceFromApi/ConvertRestApiPostToWPPost', [$this, 'addParentToPost'], 10, 3); + add_filter('Municipio/Content/ResourceFromApi/ConvertRestApiPostToWPPost', [$this, 'addParentToPost'], 10, 3); } public function preLoadAcfValue($value, $postId, $field) diff --git a/library/Content/ResourceFromApi/PostType/PostTypeResourceRequest.php b/library/Content/ResourceFromApi/PostType/PostTypeResourceRequest.php index b24e53ff5..45a4328ba 100644 --- a/library/Content/ResourceFromApi/PostType/PostTypeResourceRequest.php +++ b/library/Content/ResourceFromApi/PostType/PostTypeResourceRequest.php @@ -160,6 +160,12 @@ private static function convertRestApiPostToWPPost(stdClass $restApiPost, object $wpPost->comment_count = 0; $wpPost->filter = 'raw'; - return apply_filters('Municipio/Content/ResourceFromApi/ConvertRestApiPostToWPPost', $wpPost, $restApiPost, $localPostType); + $wpPost = apply_filters('Municipio/Content/ResourceFromApi/ConvertRestApiPostToWPPost', $wpPost, $restApiPost, $localPostType); + + if (is_a($wpPost, 'WP_Post')) { + wp_cache_add($wpPost->ID, $wpPost, 'posts'); + } + + return $wpPost; } }