我找到了解决方案。问题是行动'delete_post'。 我把它改成基于邮政状态转变“trash_to_publish”:https://codex.wordpress.org/Post_Status_Transitions
现在一切工作正常。
/**
* Deletes all posts from author of company profile.
*/
function delete_all_posts_from_author($post_id) {
global $post;
$id = $post->ID;
// Only trigger if post type is "company"
if (get_post_type($id) == "company") {
$author_id = $post->post_author;
$posts_from_author = get_posts(
array(
'posts_per_page' => -1,
'post_status' => 'publish',
'post_type' => array('event','job'),
'author' => $author_id,
'fields' => 'ids', // Only get post ID's
)
);
foreach ($posts_from_author as $post_from_author) {
wp_trash_post($post_from_author, false); // Set to False if you want to send them to Trash.
}
}
}
add_action('publish_to_trash', 'delete_all_posts_from_author', 10, 1);
作为奖励,我可以使用的功能再度用户发布的所有帖子,如果我取消删除公司简介。
/**
* Untrash posts if company profile is untrashed
*/
function untrash_all_posts_from_author($post_id) {
global $post;
$id = $post->ID;
if (get_post_type($id) == "company") {
$author_id = $post->post_author;
$posts_from_author = get_posts(
array(
'posts_per_page' => -1,
'post_status' => 'trash',
'post_type' => array('event','job'),
'author' => $author_id,
'fields' => 'ids', // Only get post ID's
)
);
foreach ($posts_from_author as $post_from_author) {
wp_untrash_post($post_from_author); // Set to False if you want to send them to Trash.
}
}
}
add_action('trash_to_publish', 'untrash_all_posts_from_author', 10, 1);
希望有所帮助。如果我犯了一个错误,请让我知道。
编辑:我已经改变了说法wp_delete_post()
到wp_trash_post()
因为wp_delete_post()
仅适用于本地的帖子,页面和附件。来自@rarst的大回答在这里:https://wordpress.stackexchange.com/questions/281877/error-after-deleting-custom-post-type-with-a-function-no-trash-used/281888#281888
看起来不错:)唯一的是wp_untrash_post()只有一个参数,但这是坚实的。 –
啊,谢谢你的提示。我会改变它:) – Cray
与@ DanielH.J相同。说 - 但对于'wp_trash_post()',@Cray。它只需要1个参数,即post_id。我认为你对'wp_delete_post()'可能会感到困惑? –