对所述职位类型使用普通查询,并检查产生的职位数量是否大于10:
public function save_post( $post_id ) {
global $post;
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return;
}
if (isset($_POST[\'post_type\']) && $_POST[\'post_type\'] == \'book\') {
$max_allowed = 10;
$terms = get_terms( [\'taxonomy\' => \'rack\'] );
$terms_ids = wp_list_pluck( $terms, \'term_id\' );
$args = [
\'post_type\' => \'book\',
\'post_status\' => \'published\',
\'tax_query\' => [
[
\'taxonomy\' => \'rack\',
\'field\' => \'term_id\',
\'terms\' => $terms_ids
]
],
\'numberposts\' => $max_allowed + 1 // just need to retrieve one more than the allowed to know whether it passed the max
];
$posts = get_posts( $args );
if( count( $posts ) > $max_allowed ) {
// Assuming this is the right method to remove actions; use the right one otherwise;
// This is to prevent infinite loop
$this->loader->remove_action( \'save_post\', $plugin_admin, \'save_post\' );
$post = [
\'ID\' => $post_id,
\'post_status\' => \'draft\'
]
// Update the post with status "draft"
wp_update_post( $post );
// Re-add the action
$this->loader->add_action( \'save_post\', $plugin_admin, \'save_post\' );
}
}
}