我在WordPress中使用了多个自定义帖子类型。 每个用户都可以将公司资料添加为名为“公司”的特定邮政类型。 如果用户拥有公司资料,他可以添加更多内容,如工作,活动等。全部定义为特定的自定义帖子类型。
用户也可能删除自己的公司资料。 如果他这样做,那么用户的所有其他帖子也应该被删除(正常帖子除外),并且URL应该被重定向(301)到主页。
我在WP文档(https://codex.wordpress.org/Plugin_API/Action_Reference/delete_post)中找到了“delete_post”操作。但我不确定如何解雇它。
有人有提示吗?
编辑:请参阅下面的答案
答案 0 :(得分:2)
我找到了解决方案。问题是行动' 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
答案 1 :(得分:1)
类似的东西:
// 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;
}
}
答案 2 :(得分:1)
这是可能的。 公司是邮政类型所以剩余的内容将是分类,所以当你删除帖子那里元,分类法将不会被删除。
<?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 ) );
}
}
?>