Advertisement
arie_cristianD

create massive post and fake view counter data

Mar 19th, 2025
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.32 KB | None | 0 0
  1.  
  2. add_action(
  3.     'admin_init',
  4.     function () {
  5.         // create_massive_posts();
  6.         // insert_popular_posts_summary();
  7.     }
  8. );
  9.  
  10.  
  11. function create_massive_posts() {
  12.     global $wpdb;
  13.     $total_posts = 100000;
  14.     $chunk_size  = 1000;
  15.     $chunks      = ceil( $total_posts / $chunk_size );
  16.  
  17.     // ID penulis, ganti dengan user ID yang valid dari database WordPress
  18.     $author_id = 1;
  19.  
  20.     for ( $i = 0; $i < $chunks; $i++ ) {
  21.         $values       = array();
  22.         $placeholders = array();
  23.         for ( $j = 0; $j < $chunk_size; $j++ ) {
  24.             $title   = 'Post Title ' . ( $i * $chunk_size + $j + 1 );
  25.             $content = 'This is the content for post ' . ( $i * $chunk_size + $j + 1 );
  26.  
  27.             // Mengatur waktu posting menjadi satu tahun yang lalu
  28.             $date = date( 'Y-m-d H:i:s', strtotime( '-1 year' ) );
  29.  
  30.             $values[] = $title;
  31.             $values[] = $content;
  32.             $values[] = $date;
  33.             $values[] = $date;
  34.             $values[] = 'publish';
  35.             $values[] = 'post';
  36.             $values[] = $author_id;
  37.  
  38.             $placeholders[] = '(%s, %s, %s, %s, %s, %s, %d)';
  39.         }
  40.  
  41.         $query = "INSERT INTO {$wpdb->prefix}posts (post_title, post_content, post_date, post_date_gmt, post_status, post_type, post_author) VALUES " . implode( ', ', $placeholders );
  42.         $wpdb->query( $wpdb->prepare( $query, ...$values ) );
  43.     }
  44. }
  45.  
  46.  
  47.  
  48. function insert_popular_posts_summary() {
  49.  
  50.     global $wpdb;
  51.  
  52.     $post_ids      = array( 671, 672, 673 ); /*  change with your list post ID */
  53.     $chunk_size    = 500;
  54.     $total_records = 10000;
  55.     $insert_data   = array();
  56.  
  57.     $view_date     = current_time( 'Y-m-d' );
  58.     $view_datetime = current_time( 'Y-m-d H:i:s' );
  59.  
  60.     for ( $i = 0; $i < $total_records; $i++ ) {
  61.         $postid        = $post_ids[ array_rand( $post_ids ) ];
  62.         $insert_data[] = $wpdb->prepare( '(%d, %s, %s)', $postid, $view_date, $view_datetime );
  63.  
  64.         if ( count( $insert_data ) >= $chunk_size ) {
  65.             $values = implode( ', ', $insert_data );
  66.             $query  = "
  67.                 INSERT INTO wp_popularpostssummary (postid, view_date, view_datetime)
  68.                 VALUES $values;
  69.             ";
  70.             $wpdb->query( $query );
  71.             $insert_data = array();
  72.         }
  73.     }
  74.  
  75.     if ( ! empty( $insert_data ) ) {
  76.         $values = implode( ', ', $insert_data );
  77.         $query  = "
  78.             INSERT INTO wp_popularpostssummary (postid, view_date, view_datetime)
  79.             VALUES $values;
  80.         ";
  81.         $wpdb->query( $query );
  82.     }
  83.  
  84.     error_log( 'Sukses memasukkan 10.000 record ke dalam wp_popularpostssummary!' );
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement