WordPress как на ладони

Как автоматически удалять старые посты, при достижении определенного количество постов?

Нужен сниппет, который будет удалять все посты, при достижении определенного количества постов на сайте. Например, при достижении 100 постов, срабатывает условия -> удаляем все посты.
Нашел в сети такой кусок, но не совсем нужные условия и не работает. Ошибка

Don't Panic
The code snippet you are trying to save produced a fatal error on line 17:

Unclosed '{' on line 2
The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.

Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.

add_action( 'publish_post', function ( $post_ID )
{
	$args = [
		'nopaging' => true,
		'fields'   => 'ids'
	];
	$posts_array = get_posts( $args );
	// Make sure that we have post ID's returned
	if ( $posts_array ) {
		// Get all post ids after the 10th post using array_slice
		$delete_posts = array_slice( $posts_array, 1000 );
		// Make sure that we actually have post ID's in the $delete_posts array
		if ( $delete_posts ) {
			foreach ( $delete_posts as $delete_post )
				wp_delete_post( $delete_post, true );
		}
	}
0
udder
1.3 года назад 8
  • 2
    el-lable620 el-lable.ru
    add_action( 'publish_post', function ( $post_ID ) {
    	$args = [
    		'post_type' => 'post',
    		'post_status' => 'publish',
    		'numberposts' => -1,
    		'fields'   => 'ids'
    	];
    	$posts_array = get_posts( $args );
    	// Make sure that we have post ID's returned
    	if ( $posts_array ) {
    		// Get all post ids after the 10th post using array_slice
    		$delete_posts = array_slice( $posts_array, 1000 );
    		// Make sure that we actually have post ID's in the $delete_posts array
    		if ( $delete_posts ) {
    			foreach ( $delete_posts as $delete_post )
    				wp_delete_post( $delete_post, true );
    		}
    	}
    } );
    Комментировать
На вопросы могут отвечать только зарегистрированные пользователи. Вход . Регистрация