Cache of paged results not flushing on updating posts
-
WP Rest Cache nicely caches JSON requests like this:
/wp-json/wp/v2/post_type?_embed=&page=1&per_page=80
However, when I update a post of the post_type above, this cache is not flushed.
That is not ideal.
-
Hi @mastababa
Thank you for using our plugin!
I tested your issue using the endpoint for posts (/wp-json/wp/v2/posts?_embed=&page=1&per_page=80
) and did some requests to multiple pages. They were all cached nicely. I then edited one of the posts and the caches were flushed as expected. So I am unable to reproduce your issue.
Did you use filters to alter the output of the endpoint (i.e. did you strip out theid
ortype
field)? If you visit the Settings > WP REST Cache > Endpoint API Caches tab, do you see the correct post type in the column Object Type for the specific endpoints?Thanks Richard.
This is for a custom post type, so there’s that.
No, I do not strip out data from the endpoints. I do *add* data to the endpoints.
No, the correct post type is not showing in the list of caches. It says ‘unknown’.
Any update on this?
I’m now encountering this same problem on another website. There, too, the post type is custom, and the ‘object type’ in the table of caches is listed as ‘unknown’.
Hi @mastababa
Sorry for the late reply, I have been really busy.
Please follow these instructions to make sure the caches aren’t listed as unknown.Thanks.
What’s the $this in that function? Keeping that in generates a fatal error.
If I change the contents of the function to the following, this appears to now work for me:
if ( $object_type !== 'unknown' ) { return $object_type; } if (count($data)) { if (count($data["data"])) { $first_object = $data["data"][0]; $object_type = get_post_type($first_object->id); } } return $object_type;
What I don’t understand is this: I just installed WP REST Cache on another site, and there, the REST cache for custom post types are correctly identified, even though I don’t do anything differently, there.
Hi @mastababa
That was an error in our readme, I just fixed it. You should fill in your namespace and rest base, in your case probably$your_namespace = 'wp/v2/';
and$your_rest_base = 'post_type';
(based on the endpoint you mentioned in your first post.
I can not explain why it doesn’t work on your site and on another site it does work. It should work out of the box, but probably some code on your website is causing this issue.Thanks.
In my example, ‘post_type’ was a placeholder for my custom post type. So, its value depends on what endpoint is requested. So, your suggested value for $your_rest_base doesn’t actually work as a consequence.
What’s the purpose of this particular test?
In one case, the endpoint I access is like so:
/wp-json/wp/v2/politician?_embed=&_fie…
Where ‘politician’ is a custom post type.
So, do I need to test for all values of $your_rest_base that represent all my custom post types?
@mastababa The test is to make sure you don’t apply this to all endpoints, but only to the specific endpoint(s) it is supposed to be applied to.
But… in your example call to the endpoint I see&_fie...
Does this happen to be&_fields=...
? If so, please make sure to includeid
andtype
in this list of fields (orid
andtaxonomy
in case of a (custom) taxonomy endpoint). These fields are needed for our plugin to correctly detemine the object type. So if you include these fields thewp_rest_cache/determine_object_type
shouldn’t be necessary.@rockfire Indeed, it is. And, indeed, I include the id of the requested objects.
@mastababa But do you also include the
type
(ortaxonomy
) field?I do not, as this does not serve a useful purpose. After all, the request itself includes the type, and is part of the URL.
So, you’re saying that if I include the type, the plugin will correctly associate the REST request with an object type?
@mastababa Yes, our plugin should be able to correctly handle the caching of the endpoint with
type
included.
- You must be logged in to reply to this topic.