我能想到的唯一方法就是使用save_post
action hook. 因为你的文章不是通过AJAX保存的,而且只有在你真正保存文章的时候,我认为那才是最好的地方。
老实说,我还没有写过很多错误消息,所以这可能是不可靠的,可能在一开始就行不通,但我从this article from SitePoint.
add_action( \'save_post\', \'my_save_post_function\' );
function my_save_function( $post_id ) {
$error = false;
$featured = get_posts(array(
\'meta_query\' => array(
array(
\'key\' => \'featured_article\',
\'compare\' => \'=\',
\'value\' => \'1\'
)
)
));
if ( count($featured) > 3 ) {
$error = new WP_Error(\'too_many_featured_articles\', \'There are too many featured articles.\');
// You could use this error to list the titles and links to edit all of the other current featured articles.
}
if ($error) {
// Here is where you would unset the ACF field, if you wanted.
}
return true;
}
这并不是为了让你度过整个过程,但希望这是你一个很好的起点。