Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_post'])) {
- // Sanitasi input data
- $post_title = sanitize_text_field($_POST['post_title']);
- $post_content = wp_kses_post($_POST['post_content']);
- $post_categories = isset($_POST['post_categories']) ? array_map('intval', $_POST['post_categories']) : array();
- // Persiapkan data post
- $new_post = array(
- 'post_title' => $post_title,
- 'post_content' => $post_content,
- 'post_status' => 'publish', // Bisa 'draft', 'publish', dll.
- 'post_author' => get_current_user_id(),
- 'post_category' => $post_categories, // ID kategori
- );
- // Insert post ke database
- $post_id = wp_insert_post($new_post);
- if ($post_id) {
- echo '<p>Post berhasil dibuat dengan ID: ' . $post_id . '</p>';
- } else {
- echo '<p>Terjadi kesalahan saat membuat post.</p>';
- }
- }
- // Ambil semua kategori untuk checkbox
- $categories = get_categories(array(
- 'hide_empty' => false, // Tampilkan kategori kosong
- ));
- // Hitung jumlah post per kategori
- $category_counts = array();
- foreach ($categories as $category) {
- $category_counts[$category->name] = $category->count;
- }
- // Ambil daftar 5 post terakhir
- $recent_posts = get_posts(array(
- 'numberposts' => 5, // Ambil 5 post terakhir
- 'post_status' => 'publish',
- ));
- ?>
- <!-- Form untuk Membuat Post -->
- <form method="POST">
- <label for="post_title">Judul Post:</label><br>
- <input type="text" id="post_title" name="post_title" required><br><br>
- <label for="post_content">Konten Post:</label><br>
- <textarea id="post_content" name="post_content" rows="5" required></textarea><br><br>
- <label>Pilih Kategori:</label><br>
- <?php foreach ($categories as $category): ?>
- <input type="checkbox" name="post_categories[]" value="<?php echo $category->term_id; ?>">
- <?php echo $category->name; ?><br>
- <?php endforeach; ?><br>
- <input type="submit" name="submit_post" value="Buat Post">
- </form>
- <hr>
- <!-- Tampilkan Jumlah Post per Kategori -->
- <h3>Jumlah Post per Kategori:</h3>
- <ul>
- <?php foreach ($category_counts as $category_name => $count): ?>
- <li><?php echo "$category_name: $count post"; ?></li>
- <?php endforeach; ?>
- </ul>
- <hr>
- <!-- Daftar Post Terakhir -->
- <h3>Post Terakhir:</h3>
- <table>
- <tr>
- <th>Post</th>
- <th>Kategori</th>
- </tr>
- <?php foreach ($recent_posts as $post): ?>
- <?php
- // Ambil kategori untuk setiap post
- $post_categories = get_the_category($post->ID);
- $category_names = array_map(function($cat) {
- return $cat->name;
- }, $post_categories);
- ?>
- <tr>
- <td><?php echo $post->post_title; ?></td>
- <td><?php echo implode(', ', $category_names); ?></td>
- </tr>
- <?php endforeach; ?>
- </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement