我正在尝试编写一个函数,当post状态设置为“发布”时,该函数将在后端自动勾选一个复选框字段。这是在我的functions.php btw中。
function featured_post(){
if( get_post_status() == 'publish' )
{
update_post_meta( $post->ID, '_featured', '1' );
}
}
我已经通过调用featured_post()将函数设置为在post预览模板中运行,但它似乎不起作用。有谁能给我指个方向吗?
发布于 2020-11-15 13:08:00
你需要使用动作钩子来使你的函数工作。在您的示例中,您需要使用如下所示的post_updated
挂钩:
function set_featured_post($post_ID, $post_after, $post_before){
if( get_post_status($post_ID) == 'publish' ){
update_post_meta( $post_ID, '_featured', '1' );
}
}
add_action( 'post_updated', 'set_featured_post', 10, 3 );
通过WordPress Codex阅读有关此挂钩的更多信息
https://stackoverflow.com/questions/64844643
复制相似问题