我正在尝试在Wordpress中添加add_action,以便在保存类型为'fep_message'的帖子时,检查与post_parent ID关联的任何'_fep_delete_by_'键,然后从wp_post_meta表中删除它们。这是我为此而构建的代码,但它不起作用:
exec
完成这项工作的适当方法是什么?
答案 0 :(得分:0)
在wordpress中使用save_post挂钩。你可以在这里找到更多信息
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
然后应该将代码更改为:
add_action('save_post', 'undelete_thread');
function undelete_thread($post_id) {
global $wpdb;
global $post;
if ($post->post_type = 'fep_message'){
$participants = fep_get_participants( $post->post_parent );
foreach( $participants as $participant )
{
$query ="SELECT meta_id FROM wp_postmeta WHERE post_id = %s and `meta_key` = '_fep_delete_by_%s'";
$queryp = $wpdb->prepare($query, array($post->post_parent, $participant));
if (!empty($queryp)) {
delete_post_meta($queryp,'_fep_delete_by_' . $participant);
}
}
}
}
答案 1 :(得分:0)
谢谢Kris,你是对的,save_post有效。我更多地简化了这个功能并且运行良好:
add_action('save_post', 'undelete_thread');
function undelete_thread($post_id) {
$post = get_post($post_id);
if ($post->post_type = 'fep_message'){
$participants = fep_get_participants( $post->post_parent );
foreach( $participants as $participant )
{
delete_post_meta($post->post_parent,'_fep_delete_by_'. $participant );
}
}
}