我正在尝试在 WordPress 中添加操作,以便在保存“fep_message”类型的帖子时,我检查是否有与 post_parent ID 关联的任何“_fep_delete_by_”键,然后从 wp_post_meta 表中删除它们。这是我为此构建的代码,但它不起作用:
add_action('publish_post', 'undelete_thread');
function undelete_thread($post_id, $post) {
global $wpdb;
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);
}
}
}
}
完成此任务的正确 Hook 是什么?
最佳答案
在 WordPress 中使用 save_post Hook 。您可以在这里找到更多信息
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);
}
}
}
}
关于php - 保存帖子类型时运行 在 WordPress 中运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46206330/