Advertisement
ssaidz

wp query month year cat

Dec 21st, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_post'])) {
  3. // Sanitasi input data
  4. $post_title = sanitize_text_field($_POST['post_title']);
  5. $post_content = wp_kses_post($_POST['post_content']);
  6. $post_categories = isset($_POST['post_categories']) ? array_map('intval', $_POST['post_categories']) : array();
  7.  
  8. // Persiapkan data post
  9. $new_post = array(
  10. 'post_title' => $post_title,
  11. 'post_content' => $post_content,
  12. 'post_status' => 'publish', // Bisa 'draft', 'publish', dll.
  13. 'post_author' => get_current_user_id(),
  14. 'post_category' => $post_categories, // ID kategori
  15. );
  16.  
  17. // Insert post ke database
  18. $post_id = wp_insert_post($new_post);
  19.  
  20. if ($post_id) {
  21. echo '<p>Post berhasil dibuat dengan ID: ' . $post_id . '</p>';
  22. } else {
  23. echo '<p>Terjadi kesalahan saat membuat post.</p>';
  24. }
  25. }
  26.  
  27. // Ambil semua kategori untuk checkbox
  28. $categories = get_categories(array(
  29. 'hide_empty' => false, // Tampilkan kategori kosong
  30. ));
  31.  
  32. // Hitung jumlah post per kategori
  33. $category_counts = array();
  34. foreach ($categories as $category) {
  35. $category_counts[$category->name] = $category->count;
  36. }
  37.  
  38. // Ambil daftar 5 post terakhir
  39. $recent_posts = get_posts(array(
  40. 'numberposts' => 5, // Ambil 5 post terakhir
  41. 'post_status' => 'publish',
  42. ));
  43. ?>
  44.  
  45. <!-- Form untuk Membuat Post -->
  46. <form method="POST">
  47. <label for="post_title">Judul Post:</label><br>
  48. <input type="text" id="post_title" name="post_title" required><br><br>
  49.  
  50. <label for="post_content">Konten Post:</label><br>
  51. <textarea id="post_content" name="post_content" rows="5" required></textarea><br><br>
  52.  
  53. <label>Pilih Kategori:</label><br>
  54. <?php foreach ($categories as $category): ?>
  55. <input type="checkbox" name="post_categories[]" value="<?php echo $category->term_id; ?>">
  56. <?php echo $category->name; ?><br>
  57. <?php endforeach; ?><br>
  58.  
  59. <input type="submit" name="submit_post" value="Buat Post">
  60. </form>
  61.  
  62. <hr>
  63.  
  64. <!-- Tampilkan Jumlah Post per Kategori -->
  65. <h3>Jumlah Post per Kategori:</h3>
  66. <ul>
  67. <?php foreach ($category_counts as $category_name => $count): ?>
  68. <li><?php echo "$category_name: $count post"; ?></li>
  69. <?php endforeach; ?>
  70. </ul>
  71.  
  72. <hr>
  73.  
  74. <!-- Daftar Post Terakhir -->
  75. <h3>Post Terakhir:</h3>
  76. <table>
  77. <tr>
  78. <th>Post</th>
  79. <th>Kategori</th>
  80. </tr>
  81. <?php foreach ($recent_posts as $post): ?>
  82. <?php
  83. // Ambil kategori untuk setiap post
  84. $post_categories = get_the_category($post->ID);
  85. $category_names = array_map(function($cat) {
  86. return $cat->name;
  87. }, $post_categories);
  88. ?>
  89. <tr>
  90. <td><?php echo $post->post_title; ?></td>
  91. <td><?php echo implode(', ', $category_names); ?></td>
  92. </tr>
  93. <?php endforeach; ?>
  94. </table>
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement