// Register WP-CLI command for deleting posts by creation date range via direct SQL if ( defined( 'WP_CLI' ) && WP_CLI ) { WP_CLI::add_command( 'sport1 delete_posts_by_date', 'sport1_delete_posts_by_date_command' ); } /** * Permanently delete all posts (any status) created within a date range via direct SQL. * * ## OPTIONS * * --date-from= * : Delete posts created on or after this date. Format: YYYY-MM-DD. * * [--date-to=] * : Delete posts created on or before this date. Format: YYYY-MM-DD. Default: today. * * [--post_type=] * : Limit to a specific post type. Default: post. * * [--dry-run] * : Show counts of what would be deleted without executing DELETE statements. * * ## EXAMPLES * * wp sport1 delete_posts_by_date --date-from=2025-12-12 * wp sport1 delete_posts_by_date --date-from=2025-12-12 --date-to=2025-12-31 * wp sport1 delete_posts_by_date --date-from=2025-12-12 --dry-run * * @param array $args Positional arguments. * @param array $assoc_args Associative arguments. */ function sport1_delete_posts_by_date_command( $args, $assoc_args ) { global $wpdb; $date_from = isset( $assoc_args['date-from'] ) ? sanitize_text_field( $assoc_args['date-from'] ) : ''; $date_to = isset( $assoc_args['date-to'] ) ? sanitize_text_field( $assoc_args['date-to'] ) : current_time( 'Y-m-d' ); $post_type = isset( $assoc_args['post_type'] ) ? sanitize_text_field( $assoc_args['post_type'] ) : 'post'; $dry_run = isset( $assoc_args['dry-run'] ); // Validate date-from format if ( ! $date_from || ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $date_from ) ) { WP_CLI::error( 'Invalid or missing --date-from. Use format YYYY-MM-DD.' ); } // Validate date-to format if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $date_to ) ) { WP_CLI::error( 'Invalid --date-to. Use format YYYY-MM-DD.' ); } $date_from_threshold = $date_from . ' 00:00:00'; $date_to_threshold = $date_to . ' 23:59:59'; WP_CLI::log( sprintf( 'Date range: %s — %s', $date_from_threshold, $date_to_threshold ) ); WP_CLI::log( sprintf( 'Post type: %s', $post_type ) ); if ( $dry_run ) { WP_CLI::warning( 'DRY RUN — no data will be deleted.' ); $post_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = %s AND post_date >= %s AND post_date <= %s", $post_type, $date_from_threshold, $date_to_threshold ) ); $meta_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->postmeta} pm INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE p.post_type = %s AND p.post_date >= %s AND p.post_date <= %s", $post_type, $date_from_threshold, $date_to_threshold ) ); $term_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->term_relationships} tr INNER JOIN {$wpdb->posts} p ON p.ID = tr.object_id WHERE p.post_type = %s AND p.post_date >= %s AND p.post_date <= %s", $post_type, $date_from_threshold, $date_to_threshold ) ); $comment_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->comments} c INNER JOIN {$wpdb->posts} p ON p.ID = c.comment_post_ID WHERE p.post_type = %s AND p.post_date >= %s AND p.post_date <= %s", $post_type, $date_from_threshold, $date_to_threshold ) ); WP_CLI::log( sprintf( 'Posts to delete: %d', $post_count ) ); WP_CLI::log( sprintf( 'Postmeta rows to delete: %d', $meta_count ) ); WP_CLI::log( sprintf( 'Term relationships to delete: %d', $term_count ) ); WP_CLI::log( sprintf( 'Comments to delete: %d', $comment_count ) ); WP_CLI::success( 'DRY RUN complete. Run without --dry-run to execute.' ); return; } WP_CLI::log( 'Starting deletion...' ); // Delete postmeta for matching posts $start = microtime( true ); $deleted_meta = $wpdb->query( $wpdb->prepare( "DELETE pm FROM {$wpdb->postmeta} pm INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE p.post_type = %s AND p.post_date >= %s AND p.post_date <= %s", $post_type, $date_from_threshold, $date_to_threshold ) ); WP_CLI::log( sprintf( 'Deleted postmeta rows: %d (%.2fs)', $deleted_meta, microtime( true ) - $start ) ); if ( $wpdb->last_error ) { WP_CLI::warning( sprintf( 'DB error after postmeta DELETE: %s', $wpdb->last_error ) ); } // Delete term relationships for matching posts $start = microtime( true ); $deleted_terms = $wpdb->query( $wpdb->prepare( "DELETE tr FROM {$wpdb->term_relationships} tr INNER JOIN {$wpdb->posts} p ON p.ID = tr.object_id WHERE p.post_type = %s AND p.post_date >= %s AND p.post_date <= %s", $post_type, $date_from_threshold, $date_to_threshold ) ); WP_CLI::log( sprintf( 'Deleted term relationships: %d (%.2fs)', $deleted_terms, microtime( true ) - $start ) ); if ( $wpdb->last_error ) { WP_CLI::warning( sprintf( 'DB error after term_relationships DELETE: %s', $wpdb->last_error ) ); } // Delete comments for matching posts $start = microtime( true ); $deleted_comments = $wpdb->query( $wpdb->prepare( "DELETE c FROM {$wpdb->comments} c INNER JOIN {$wpdb->posts} p ON p.ID = c.comment_post_ID WHERE p.post_type = %s AND p.post_date >= %s AND p.post_date <= %s", $post_type, $date_from_threshold, $date_to_threshold ) ); WP_CLI::log( sprintf( 'Deleted comments: %d (%.2fs)', $deleted_comments, microtime( true ) - $start ) ); if ( $wpdb->last_error ) { WP_CLI::warning( sprintf( 'DB error after comments DELETE: %s', $wpdb->last_error ) ); } // Delete the posts themselves $start = microtime( true ); $deleted_posts = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->posts} WHERE post_type = %s AND post_date >= %s AND post_date <= %s", $post_type, $date_from_threshold, $date_to_threshold ) ); WP_CLI::log( sprintf( 'Deleted posts: %d (%.2fs)', $deleted_posts, microtime( true ) - $start ) ); if ( $wpdb->last_error ) { WP_CLI::warning( sprintf( 'DB error after posts DELETE: %s', $wpdb->last_error ) ); } WP_CLI::success( 'Done.' ); } euroleague Archives - SPORT1

Es scheint, dass wir nicht finden können, was du suchst. Vielleicht kann die Suche helfen.