Deleting the record
-
Once, I have deleted the record, The record still exist in my front end. How can I fix this?
-
I face similar issue:
Once a record related to an object type with custom endpoint is deleted, it is still cached and the cached endpoint is not flushing automatically. I noticed the auto-flushing of the cache is happening whenever an object’s record is created or edited. I need similar solution for the delete action as well. Any solution?
Hi @radha2914 and @shyamla
Thank you for using our plugin!
If we are talking about the default WordPress endpoints, the cache record should be automatically deleted by our plugin. So for instance if you delete a page with
ID = 42
and there is a cache for/wp-json/wp/v2/pages/42
then this cache should automatically be deleted by our plugin. Is this not the case for you?If we are talking about a custom endpoint then there are multiple solutions / causes for the problem:
- Does your endpoint contain a (custom) post type or (custom) taxonomy? Then please make sure the plugin can detect the correct object type. See our FAQ about how you can help the plugin detect it correctly. Once that is done the plugin should be able to automatically flush or delete the cache (depending whether it was edited or deleted).
- If that is not possible for you, you could trigger the deletion of the cache yourself. Upon deletion of the record you can tell the plugin to delete all related caches:
\WP_REST_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_related_caches( 42, 'my_custom_post_type', true);
Please let us know if you need more help, or when it is not working (correctly) for you
Thanks for your reply. Our custom endpoint looks something similar to this:
https://<domain>/wp-json/<custom_namespace>/<custom_name>/faq
We have added from the plugin FAQ like this:
function wprc_add_endpoint( $allowed_endpoints ) { if ( ! isset( $allowed_endpoints[ '<custom_namespace>' ] ) || ! in_array( 'posts', $allowed_endpoints[ '<custom_namespace>' ] ) ) { $allowed_endpoints[ '<custom_namespace>' ][] = '<custom_name>'; } return $allowed_endpoints; } add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_endpoint', 10, 1);
Adding this helped the plugin to identify the custom object (post) type, thereby allowing the plugin to flush the cache whenever the post/record is created or edited. But not while deletion. We still needed to wait till the next scheduled flush (We set that as 1 hour).
Note: We are not using any other custom code to perform the CRUD operations.
-
This reply was modified 5 years, 1 month ago by
shyamla.
Hi @shyamla
Well that is step 1, you have told the plugin to also cache your custom endpoint. But when you visit Settings > WP REST Cache > Endpoint API Caches in you wp-admin and search for a cache record for your custom endpoint, what do you see under “Object Type”? If it says
unknown
it still isn’t able to determine the correct object type.
If it displays the correct object type, then we have to look further as to why it is not being deleted correctly. After deleting the record how do you determine the cache is still there? Did you look at Settings > WP REST Cache > Endpoint API Caches in you wp-admin and was is still there?Yes, it is determining the correct object type. But once we delete the record, both in the endpoint and in the cache details’ cache data, we could notice the data is still cached.
Hi @shyamla
And that object type, is it a (custom) post type or a (custom) taxonomy? (Just double-checking)
I just did some tests and for me the endpoint caches are all being deleted just as expected. So I suspect there is some other code which is causing this behaviour. We can try to have the cache deleted even sooner by changing the priority of our
add_action
. Could you try and edit /includes/class-plugin.php on line 144 it says:
add_action( 'delete_post', [ $caching, 'delete_post' ] );
could you change that to:
add_action( 'delete_post', [ $caching, 'delete_post' ], 1 );
and then retry if the cache is deleted once the post is deleted?Hi @rockfire
We are using the custom post type. We have changed the code (line 144) and deleted the record in the custom post type. still, we could notice the data is cached. It did not clear the cache as well.
Hi @shyamla
Sorry for not getting back to you any sooner. We did some extensive testing but were unable to reproduce your situation. Unfortunately that makes it impossible for us to determine the cause and implement a fix for it.
Did you by any chance find the cause?Hi Richard,
Thank you so much for your reply, I could not find the cause. I have followed all your instructions, uninstalled other plugins and cross-checked everything. Still, we are facing the same issue.
- The topic ‘Deleting the record’ is closed to new replies.