EDIT: Based on @TimHallman's answer I have a follow-up question, please see the bottom of this post.
I am trying to do something that is way over my head and the more I think of it, the more questions I end up with.
This is the scenario I have:
- Custom posts of type golfcourse, each presenting a golf course.
- Custom posts of type clubnews.
- Clubnews has the term clubnewsowner related to them.
- The clubnewsowner taxonomies are identical to the golfcourses.
What I do is that (using a self made shortcode) I check, on each golfcourse-post, if one or more posts of type clubnews with taxonomy related to the spesific golfcourse exists. If yes, show the clubnews-post(s) on the golfcourse-post.
This works as I want it to.
I do, however, use the WP-Rocket caching plugin on this website. And because the clubnews-posts are added (to the golfcourse-posts) using php only (no ajax), WP-Rocket has no clue the content has changed on the golfcourse-post whenever a clubnews post has been added, updated or deleted. This means that I need to do a manual cache-clearing of the related golfcourse-post whenever the mentioned scenario occurs.
Luckily, WP-Rocket has a function for this:
//clean post with ID 5
rocket_clean_post( 5 );
I have managed to create some kind of pseudo-code:
function clearPageCacheBasedOnTaxOfClubnews() {
if ( ( clubnews is created ) || ( clubnews is updated ) || ( clubnews is deleted ) ) {
$customPost = clubnewsPostID;
// The result here is always only one taxonomy
$taxonomyOfCustompPost = get_post_taxonomies( $customPost );
switch ($taxonomyOfCustompPost) {
case 'golfcourseOne': rocket_clean_post( 5 );
break;
case 'golfcourseTwo': rocket_clean_post( 8 );
break;
}
}
}
add_action( 'when?', 'clearPageCacheBasedOnTaxOfClubnews', 10, ?);
I think the above code will work, but there is a lot of things I am not sure of here:
- How do I get the ID of the clubnews post being created/updated/deleted?
- How do I check if it is actually being created/updated/deleted?
- At this point my head starts spinning, and I am no longer sure what I am wondering...
ANY help is appreciated!
Follow-up question:
I do believe @Tim Hallman solves at least two thirds of this (I need another action for when a clubnews custom post gets deleted) with his answer below.
I, however cannot make this work. My code produces a white screen of death, whitout generating any php errors. What I think happens is that WP Rocket uses all of the resources on the server when clearing the cache on individual posts. I'm not sure though.
This is the code:
add_action( 'save_post', 'clearPageCacheBasedOnTaxOfClubnews');
function clearPageCacheBasedOnTaxOfClubnews($post_id) {
/* Is has_term() used correctly here? In the codex it says that the
* taxonomy parameter is optional, other places on the Internet claims
* the opposite...
*/
if ( has_term('clubnewsowner', '', $post_id ) {
// The result here is always only one taxonomy
$taxonomyOfCustompPost = get_post_taxonomies( $post_id );
/* The codex says get_post_taxonomies() returns an array. The code
* on the line below produces a php fatal error though.
*/
$taxonomyOfCustompPost = $taxonomyOfCustompPost[0];
/* This is where the connection between the taxonomy of the
* Clubnews custom posts and the golf course pages happens
*/
switch ($taxonomyOfCustompPost){
case 'Course One': $courseID = 123; break;
case 'Course Two': $courseID = 234; break;
case 'Course Three': $courseID = 345; break;
...
}
//This cleans the cache of the selected post
rocket_clean_post( $courseID );
}
}
I have tried this in different variants, and either I get a fatal error because of the $taxonomyOfCustompPost = taxonomyOfCustompPost[0]; or I get a white screen of death whitout any php errors at all.
Any suggestions on where to go on from here?
save_post
hook – Tim Hallman Mar 8 '18 at 8:17WP_DEBUG
set to true and use the query monitor plugin – Mark Kaplun Mar 9 '18 at 6:47