我正在使用一个自定义的元字段,来指定哪篇文章将作为特色文章出现在首页。
此元字段是通过ACF声明的真/假值。
我希望这个条件只适用于一篇文章,即当用户将一篇文章声明为“特色”并保存它时,所有其他选中此元值的文章(理论上只有最后一篇)都应该被关闭。
为了实现这一点,我声明了一个附加到“save\\u post”操作的函数,对于旧的特色帖子,该函数会将这个特定的帖子元值变为“false”。
这是我的近似值,但我遗漏了一些东西,因为它根本不起作用。
function only_one_agenda_featured( $post_id ) {
// If this isn\'t a \'agenda\' post, don\'t update it.
if ( \'agenda\' != $post->post_type ) {
return;
}
// Stop when it is an autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return;
}
// Prevent quick edit from clearing custom fields
if (defined(\'DOING_AJAX\') && DOING_AJAX) {
return;
}
$post_meta_value = get_post_meta( $post_id, \'agenda_featured\', TRUE );
if ( $post_meta_value == \'1\' ) {
$args = array(
\'meta_key\' => \'agenda_featured\',
\'meta_value\' => \'1\',
\'post_type\' => \'agenda\',
\'post__not_in\' => array($post_id),
);
$posts_to_update = get_posts( $args );
foreach ( $posts_to_update as $post_to_update ) {
update_post_meta( $post_to_update->ID, \'agenda_featured\', \'0\', \'1\' );
}
}
}
add_action( \'save_post\', \'only_one_agenda_featured\' );
任何帮助都将不胜感激。