我的理解是,您希望对您的类别设置帖子数量限制,以便每个类别都有指定数量的帖子。很长一段时间后,我试图回答WordPress SE上的一个问题,所以我希望我能理解。
说明:类别没有元数据,因此您需要自己的自定义表来存储类别限制。对自定义表的读写应该在自定义元函数中进行抽象。例如get_post_count_limit
. 一旦get和set函数就绪,那么您就可以连接到所需的WordPress操作(正如您提到的publish post,所以在下面的代码中,我使用了publish_post
操作)。
在连接到的函数中publish_post
操作中,您可以简单地循环分配给帖子的所有类别,使用自定义函数检查它们的限制,然后检查category\\u count属性。如果category\\u计数超过限制,只需获取最旧的帖子并从中删除category即可。
代码
<?php
/**
* Plugin Name: Category Post Count Limit
* Description: Allows to set limit on number of posts in a category
* Author: Hameedullah Khan
**/
add_action( \'publish_post\', \'cpcl_check_post_limit\' );
function cpcl_check_post_limit( $post_id ) {
$cat_ids = wp_get_post_categories( $post_id );
foreach( $cat_ids as $cat_id ) {
$cat = get_category( $cat_id );
$post_count_limit = get_post_count_limit( $cat_id );
if ( $post_count_limit < 1 ) { // limit lower then 1 means unlimited.
continue;
}
$category_count = $cat->category_count;
if ( $category_count > $post_count_limit ) {
$numposts = $category_count - $post_count_limit;
$query = new WP_Query( array( \'category__in\' => array( $cat_id ), \'orderby\' => \'date\', \'posts_per_page\' => $numposts, \'order\' => \'ASC\' ) );
while ($query->have_posts() ) {
$query->next_post();
$cp_cat_ids = wp_get_post_categories( $query->post->ID );
unset( $cp_cat_ids[array_search( $cat_id, $cp_cat_ids )] );
$cp_cat_ids = array_values( $cp_cat_ids );
wp_set_post_categories( $query->post->ID, $cp_cat_ids );
}
wp_reset_postdata();
}
}
}
add_action( \'category_add_form_fields\', \'cpcl_field_add\', 10, 1);
function cpcl_field_add( $cat ) {
?>
<div class="form-field">
<label for="post-count">Post Count Limit</label><input type="text" name="post-count" type="text" /></label>
</div>
<?php
}
add_action( \'category_edit_form_fields\', \'cpcl_field_edit\', 10, 1 );
function cpcl_field_edit( $cat ) {
$post_count = get_post_count_limit( $cat->term_id );
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="post-count">Post Count Limit</label>
</th>
<td>
<input type="text" name="post-count" type="text" value="<?php echo $post_count; ?>" /></label>
</td>
</tr>
<?php
}
add_action( \'created_category\', \'cpcl_save_post_count_limit\', 10, 1 );
add_action( \'edited_category\', \'cpcl_save_post_count_limit\', 10, 1 );
function cpcl_save_post_count_limit( $cat_id ) {
if (current_user_can( \'manage_categories\' ) && isset( $_POST[\'post-count\'] ) ) {
$post_count_limit = ( int ) $_POST[\'post-count\'];
update_post_count_limit( $cat_id, $post_count_limit );
}
}
function get_post_count_limit( $cat_id, $return_null = false ) {
global $wpdb;
$table_name = $wpdb->prefix . \'cat_post_count_limit\';
$cpcl_meta = $wpdb->get_row( "SELECT * FROM $table_name WHERE cat_id = $cat_id" );
if ( ! $cpcl_meta ) {
if ( $return_null && $cpcl_meta == null ) {
return null;
} else {
return 0;
}
} else {
return $cpcl_meta->post_count;
}
}
function update_post_count_limit( $cat_id, $post_count_limit ) {
global $wpdb;
$table_name = $wpdb->prefix . \'cat_post_count_limit\';
if ( get_post_count_limit( $cat_id, true) == null ) {
$wpdb->insert(
$table_name,
array(
\'cat_id\' => $cat_id,
\'post_count\' => $post_count_limit
),
array(
\'%d\',
\'%d\'
)
);
return $wpdb->insert_id;
} else {
return $wpdb->update(
$table_name,
array(
\'post_count\' => $post_count_limit
),
array(
\'cat_id\' => $cat_id
),
array( \'%d\' ),
array( \'%d\' )
);
}
}
function cpcl_initialize_categories() {
global $wpdb;
$table_name = $wpdb->prefix . \'cat_post_count_limit\';
$sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
`cpcl_id` int(11) NOT NULL AUTO_INCREMENT,
`cat_id` int(11) NOT NULL,
`post_count` int(11) NOT NULL,
PRIMARY KEY (`cpcl_id`)
)";
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
dbDelta( $sql );
}
register_activation_hook( __FILE__, \'cpcl_initialize_categories\' );
?>
Note: 将上述代码用作插件,这样就可以在创建插件时创建自定义表。如果不想将其用作插件,则必须手动创建自定义表。
非常期待关于改进我的答案和/或代码的建议。