WordPress:删除一个帖子后的所有帖子已被删除
我在WordPress中使用了多个自定义帖子类型。 每个用户都可以将公司资料添加为称为“公司”的特定帖子类型。 如果用户有公司资料,他可以添加更多内容,如工作,活动等。全部定义为特定的自定义帖子类型。WordPress:删除一个帖子后的所有帖子已被删除
用户也有可能删除自己的公司资料。 如果他这样做了,那么用户的所有其他帖子也应该被删除(普通帖子除外),并且该URL应该被重定向(301)到主页。
我在WP文档(https://codex.wordpress.org/Plugin_API/Action_Reference/delete_post)中找到了操作“delete_post”。但我不知道如何解雇它。
有没有人有提示?
编辑:见我的回答如下
我找到了解决方案。问题是行动'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
喜欢的东西:
// Add action trigger
add_action('delete_post', 'delete_other_custom_posts', 10);
function delete_other_custom_posts($post_id) {
global $wpdb;
$post = get_post($post_id);
// Only trigger if post type is "company"
if ($post->post_type == "company") {
$author_id = $post->post_author;
// Find other custom posts' ids by the same user
$posts_to_delete = $wpdb->get_col(
$wpdb->prepare(
"SELECT ID FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_author = %d AND post_type IN (<put your custom post types' slugs here>)",
$author_id
), ARRAY_A
);
if ($posts_to_delete) {
foreach ($posts_to_delete as $post_delete_id) {
// Delete the custom post
wp_delete_post($post_delete_id);
}
}
// Redirect
wp_redirect(<page you want to redirect to>);
exit;
}
}
这是可能的。 公司是邮政类型,所以剩余的内容将是分类,所以当你删除后有元,分类不会被删除。
<?php
add_action('admin_init', 'codex_init');
function codex_init() {
add_action('delete_post', 'codex_sync', 10);
}
function codex_sync($pid) {
global $wpdb;
if ($wpdb->get_var($wpdb->prepare('SELECT post_id FROM .'$wpdb->prefix.'postmeta WHERE post_id = %d', $pid))) {
$wpdb->query($wpdb->prepare('DELETE FROM '.$wpdb->prefix.'postmeta WHERE post_id = %d', $pid));
}
}
?>
谢谢,但其余的内容也是一种帖子类型不是一个分类 – Cray
看起来不错:)唯一的是wp_untrash_post()只有一个参数,但这是坚实的。 –
啊,谢谢你的提示。我会改变它:) – Cray
与@ DanielH.J相同。说 - 但对于'wp_trash_post()',@Cray。它只需要1个参数,即post_id。我认为你对'wp_delete_post()'可能会感到困惑? –