Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: clean the cache of a specific page for custom post types #257

Closed
benoitchantre opened this issue Aug 2, 2014 · 15 comments

Comments

@benoitchantre
Copy link

In the case you display custom posts in a page, you are forced to manually clean the cache for the whole site to reflect the changes.

It would be nice to have a way to automatically clean the cache for a specific page related to custom post types.

Maybe a new setting in the options or a function that we can use when save_post is triggered?

@raamdev
Copy link
Contributor

raamdev commented Aug 3, 2014

@benoitchantre I'm not clear on what you're asking for here. Are you wanting an option to purge all cache files for a specific Custom Post Type? You say that you want to "clean the cache for a specific page related to custom post types", but what would 'related' mean here? How is the page related to the custom post types and how would Quick Cache know that the page is related?

@jaswrks
Copy link

jaswrks commented Aug 3, 2014

Same here. Sounds like an interesting idea, but some more detail would be really helpful. Just a few more specifics (i.e. answers to Raam's questions) to help clarify the intent.

@benoitchantre
Copy link
Author

The context
I use a specific page template to display a list of a type of custom posts. I have preferred this solution over an archive page, because I can add some text, modify the title, etc from the admin.

The issue
When I add a new custom post or when I edit one, the changes cannot be seen on the website before I clear the cache. At the moment, I need to clear the cache for the whole website.

The solution
It would be better to automatically clean the cache of the page that display the posts instead the clean the cache of the whole website.
Maybe we could have an additional setting in quick cache to link custom post types with specific pages.
For example: each custom post type would be listed with a text field to let us put ID's of the pages that are using this custom post type. Then, each time you edit or add a custom post, only the cache of the pages that are related is cleaned.

@raamdev
Copy link
Contributor

raamdev commented Aug 4, 2014

@benoitchantre Thank you very much for the additional information. That was very helpful! I see now what you're trying to do.

First, I'd like to point you to a similar GitHub issue where someone was looking for a way to purge the cache for a specific page whenever a specific Custom Post Type was saved. I provided example code that you can use to achieve this: See issue #73.


Regarding your feature request: There's another feature request open for creating a place to specify user-defined URIs to purge with post cache (#111).

It sounds like if that feature request was implemented, you'd be able to simply specify the URI to your page (e.g., /my-archive-page-for-a-custom-post-type/) and then whenever the post cache (not the entire cache) is cleared, that specific page cache would also be cleared. That means if you save a Custom Post Type or create a new one, it would automatically purge your specified page.

Of course that wouldn't be quite a granular as it could be (i.e., if you saved a non-Custom Post Type, it would still clear your Custom Post Type archive page), but it sounds like that might still be better than requiring custom code to be implemented.

What do you think? Would a place to specify URIs to purge whenever the post cache is cleared (i.e., whenever a post is saved) work for you?

@benoitchantre
Copy link
Author

@raamdev Thanks for your detailed answer.

I can edit my theme with the solution you provided in issue #73, because I use a custom theme. If this was a premium theme, I would have to make a child theme to keep that behavior after an update of the theme.

I think it is a better solution to have a setting in quick cache to define which cache should be cleared when a custom post is saved. It avoids to edit the theme and keeps everything centralized in quick cache.

The feature request #111 would work for me. It would be a lot better than to erase the cache of the whole website. For a website with a lot of visits and a lot of posts published regulary, it would be better to have a more granular solution, but that is not my case.

@raamdev
Copy link
Contributor

raamdev commented Aug 4, 2014

@benoitchantre Thank you. I will close this feature request for now and instead focus on getting #73 completed. If I see demand for a more granular solution than #73, I will reopen this. :)

@benoitchantre
Copy link
Author

No problem. Thanks for the support!

Le 4 août 2014 à 23:22, Raam Dev [email protected] a écrit :

@benoitchantre Thank you. I will close this feature request for now and instead focus on getting #73 completed. If I see demand for a more granular solution than #73, I will reopen this. :)


Reply to this email directly or view it on GitHub.

@benoitchantre
Copy link
Author

@raamdev I have tested the solution described in #73, but that doesn't work for me. Maybe I have missed something.

I want to purge the cache the page ID 10 when I save any custom post of type ts_animal
The cache of the post is cleaned, but not the cache of the page.

/**
 * Clear the cache for the animal page
 */
add_action( 'clear_cache_for_animal_page', 'save_post_ts_animal', 10, 1 );

function clear_cache_for_animal_page( ) {
    $GLOBALS['quick_cache']->auto_purge_post_cache(10);
}

Is it because the name of my custom post type contains an underscore?

@raamdev
Copy link
Contributor

raamdev commented Aug 18, 2014

The cache of the post is cleaned, but not the cache of the page.

I'm not sure I understand that statement. What's the difference between the 'post' and the 'page' in this case? In WordPress (in the code) a Post and a Page are essentially the same thing and they all have an ID. So you couldn't have a Post with ID 10 and a Page with ID 10; they would need to be different IDs.

Are you referring to an archive view for a Custom Post Type? For example, assuming the post type name is ts_animal, you should be able to visit http://example.com/ts_animal/ to view an archive of all posts with the post type ts_animal.

Are you trying to clear the Archive View for ts_animal?

@benoitchantre
Copy link
Author

I use a custom page template to display my custom post types ts_animal.

It offers me more flexibility in the blackend than an archive page.

The custom post type ts_animal is a post about an animal: a definition.
The page I have mentioned is a collection of animals with links to each animals.

When I save a custom post, the function should clear the cache of the page (ID = 10), but this doesn't happen.

I hope my explanation is better than before.

Le 18 août 2014 à 20:16, Raam Dev [email protected] a écrit :

The cache of the post is cleaned, but not the cache of the page.

I'm not sure I understand that statement. What's the difference between the 'post' and the 'page' in this case? In WordPress (in the code) a Post and a Page are essentially the same thing and they all have an ID. So you couldn't have a Post with ID 10 and a Page with ID 10; they would need to be different IDs.

Are you referring to an archive view for a Custom Post Type? For example, assuming the post type name is ts_animal, you should be able to visit http://example.com/ts_animal/ to view an archive of all posts with the post type ts_animal.

Are you trying to clear the Archive View for ts_animal?


Reply to this email directly or view it on GitHub.

@raamdev
Copy link
Contributor

raamdev commented Aug 23, 2014

On this line:

add_action( 'clear_cache_for_animal_page', 'save_post_ts_animal', 10, 1 );

Try changing save_post_ts_animal to save_post. Does the cache for your page with ID 10 get cleared when you save any post (including a regular Post)?

@benoitchantre
Copy link
Author

I have changed save_post_ts_animal to save_post but I have noticed no change.
The cache of the current post, the cache of the archive for the custom taxonomy and the cache of the sitemap are cleared, but not the cache of the page ID 10.
When I update a regular post, the cache of the page ID 10 is not cleared.

@jaswrks
Copy link

jaswrks commented Aug 24, 2014

@benoitchantre I just wanted to point out a problem in this code snippet...

/**
 * Clear the cache for the animal page
 */
add_action( 'clear_cache_for_animal_page', 'save_post_ts_animal', 10, 1 );

function clear_cache_for_animal_page( ) {
    $GLOBALS['quick_cache']->auto_purge_post_cache(10);
}

add_action( 'clear_cache_for_animal_page', 'save_post_ts_animal', 10, 1 );

Should be...

add_action( 'save_post_ts_animal', 'clear_cache_for_animal_page', 10, 1 );

See also: http://codex.wordpress.org/Function_Reference/add_action
Or (updated): http://developer.wordpress.org/reference/functions/add_action/

<?php add_action( $hook, $function_to_add, $priority, $accepted_args ); ?>

Where $function_to_add in your example is: clear_cache_for_animal_page

@benoitchantre
Copy link
Author

@jaswsinc thanks for your comment. It was a basic error...

Now everything work :-)
Thanks everybody for the support.

@raamdev
Copy link
Contributor

raamdev commented Aug 26, 2014

@benoitchantre Sorry for the silly mistake! I'm glad to hear it's working now. :)

@jaswsinc Thanks for pointing out the error!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants