Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action(
- 'admin_init',
- function () {
- // create_massive_posts();
- // insert_popular_posts_summary();
- }
- );
- function create_massive_posts() {
- global $wpdb;
- $total_posts = 100000;
- $chunk_size = 1000;
- $chunks = ceil( $total_posts / $chunk_size );
- // ID penulis, ganti dengan user ID yang valid dari database WordPress
- $author_id = 1;
- for ( $i = 0; $i < $chunks; $i++ ) {
- $values = array();
- $placeholders = array();
- for ( $j = 0; $j < $chunk_size; $j++ ) {
- $title = 'Post Title ' . ( $i * $chunk_size + $j + 1 );
- $content = 'This is the content for post ' . ( $i * $chunk_size + $j + 1 );
- // Mengatur waktu posting menjadi satu tahun yang lalu
- $date = date( 'Y-m-d H:i:s', strtotime( '-1 year' ) );
- $values[] = $title;
- $values[] = $content;
- $values[] = $date;
- $values[] = $date;
- $values[] = 'publish';
- $values[] = 'post';
- $values[] = $author_id;
- $placeholders[] = '(%s, %s, %s, %s, %s, %s, %d)';
- }
- $query = "INSERT INTO {$wpdb->prefix}posts (post_title, post_content, post_date, post_date_gmt, post_status, post_type, post_author) VALUES " . implode( ', ', $placeholders );
- $wpdb->query( $wpdb->prepare( $query, ...$values ) );
- }
- }
- function insert_popular_posts_summary() {
- global $wpdb;
- $post_ids = array( 671, 672, 673 ); /* change with your list post ID */
- $chunk_size = 500;
- $total_records = 10000;
- $insert_data = array();
- $view_date = current_time( 'Y-m-d' );
- $view_datetime = current_time( 'Y-m-d H:i:s' );
- for ( $i = 0; $i < $total_records; $i++ ) {
- $postid = $post_ids[ array_rand( $post_ids ) ];
- $insert_data[] = $wpdb->prepare( '(%d, %s, %s)', $postid, $view_date, $view_datetime );
- if ( count( $insert_data ) >= $chunk_size ) {
- $values = implode( ', ', $insert_data );
- $query = "
- INSERT INTO wp_popularpostssummary (postid, view_date, view_datetime)
- VALUES $values;
- ";
- $wpdb->query( $query );
- $insert_data = array();
- }
- }
- if ( ! empty( $insert_data ) ) {
- $values = implode( ', ', $insert_data );
- $query = "
- INSERT INTO wp_popularpostssummary (postid, view_date, view_datetime)
- VALUES $values;
- ";
- $wpdb->query( $query );
- }
- error_log( 'Sukses memasukkan 10.000 record ke dalam wp_popularpostssummary!' );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement