EDIT: <根据@TimHallman的回答,我有一个后续问题,请看这篇文章的底部
我正试图做一些超出我头脑的事情,我想得越多,我最终会遇到越多的问题。
这就是我的情况:
类型的自定义帖子golfcourse, 每个人都有一个高尔夫球场clubnews.clubnewsowner 与他们相关我所做的是(使用自制的短代码)在每个高尔夫球场帖子上检查是否存在一个或多个clubnews类型的帖子,其分类与特定高尔夫球场相关。如果是,请在高尔夫球场帖子上显示俱乐部新闻帖子。
这是我想要的。
然而,我确实在这个网站上使用了WP Rocket缓存插件。而且,由于clubnews帖子(到golfcourse帖子)仅使用php(无ajax)添加,所以每当添加、更新或删除clubnews帖子时,WP Rocket都不知道golfcourse帖子上的内容发生了变化。这意味着每当出现上述场景时,我需要手动清除相关的golfcourse帖子。
幸运的是,WP Rocket具有以下功能:
//clean post with ID 5
rocket_clean_post( 5 );
我成功地创建了一些伪代码: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, ?);
我认为上面的代码可以工作,但这里有很多事情我不确定:如何获取正在创建/更新/删除的clubnews帖子的ID
<小时>
Follow-up question:
我相信@TimHallman用下面的答案至少解决了三分之二的问题(当clubnews自定义帖子被删除时,我需要另一个动作)。一、 然而,我们无法做到这一点。我的代码生成一个白色的死亡屏幕,不会生成任何php错误。我认为发生的是,当清除单个帖子上的缓存时,WP Rocket使用了服务器上的所有资源。不过我不确定。
代码如下:
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 );
}
}
我曾在不同的变体中尝试过这一点,但要么因为$taxonomyofcustompost=taxonomyofcustompost[0]而出现致命错误;或者我会得到一个完全没有php错误的白色死亡屏幕。你有什么建议吗?