Professors Post Type
Tujuan
- Membuat custom post type Professor
- Setup template single professor
- Menambahkan relasi professor ↔ program
- Menampilkan related professors di halaman program
- Mengenal
wp_reset_postdata()
📝 Catatan: Saat membuat Professor post type, pastikan menyertakan
'show_in_rest' => trueagar Block Editor aktif.
Langkah 1: Register Post Type Professor
Buka wp-content/mu-plugins/university-post-types.php, tambahkan di dalam fungsi university_post_types():
// Professor post type
register_post_type('professor', array(
'show_in_rest' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'public' => true,
'labels' => array(
'name' => 'Professors',
'add_new_item' => 'Add New Professor',
'edit_item' => 'Edit Professor',
'all_items' => 'All Professors',
'singular_name' => 'Professor'
),
'menu_icon' => 'dashicons-welcome-learn-more'
));Perbedaan dari Post Type Lain
| Fitur | Event | Program | Professor |
|---|---|---|---|
has_archive | ✅ | ✅ | ❌ (tidak ada archive) |
rewrite | 'events' | 'programs' | Tidak perlu |
supports | title, editor, excerpt | title, editor | title, editor, thumbnail |
| Icon | dashicons-calendar | dashicons-awards | dashicons-welcome-learn-more |
💡 Professor tidak perlu archive karena pengunjung menemukan professor melalui program, campus, atau search — bukan lewat halaman listing
/professors.
⚠️ Jangan lupa: Settings → Permalinks → Save Changes setelah register post type baru!
Langkah 2: Buat Post Contoh
- Dr. Meowsalot (foto kucing)
- Dr. Barksalot (foto anjing)
Langkah 3: Template Single Professor
Buat file: single-professor.php
Copy dari single-event.php, lalu modifikasi — hapus metabox (professor tidak butuh link "back to archive"):
<?php get_header(); ?>
<div class="page-banner">
<div class="page-banner__bg-image" style="background-image: url(<?php echo get_theme_file_uri('/images/ocean.jpg') ?>);">
</div>
<div class="page-banner__content container container--narrow">
<h1 class="page-banner__title"><?php the_title(); ?></h1>
<div class="page-banner__intro">
<p><?php the_title(); ?></p>
</div>
</div>
</div>
<div class="container container--narrow page-section">
<div class="generic-content">
<?php the_content(); ?>
</div>
</div>
<?php get_footer(); ?>Langkah 4: Tambah Related Programs ke Professor
Update ACF Field Group Location
- Custom Fields → Edit Related Program(s) field group
- Di bagian Location, klik or
- Tambahkan: Post Type = Professor
- Sekarang field muncul di Event dan Professor
Update Heading di Template
Di single-professor.php, ganti teks "Related Program(s)" menjadi "Subject(s) Taught":
echo '<h2 class="headline headline--medium">Subject(s) Taught</h2>';Langkah 5: Tampilkan Related Professors di Single Program
Di single-program.php, tambahkan custom query di atas kode existing related events:
<?php
$relatedProfessors = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'professor',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'related_programs',
'compare' => 'LIKE',
'value' => '"' . get_the_ID() . '"'
)
)
));
if ($relatedProfessors->have_posts()) {
echo '<hr class="section-break">';
echo '<h2 class="headline headline--medium">' . get_the_title() . ' Professors</h2>';
echo '<ul class="professor-cards">';
while ($relatedProfessors->have_posts()) {
$relatedProfessors->the_post(); ?>
<li class="professor-card__list-item">
<a class="professor-card" href="<?php the_permalink(); ?>">
<img class="professor-card__image" src="<?php the_post_thumbnail_url(); ?>">
<span class="professor-card__name"><?php the_title(); ?></span>
</a>
</li>
<?php }
echo '</ul>';
}
wp_reset_postdata();
?>wp_reset_postdata() — Sangat Penting!
Masalah
Saat menjalankan multiple custom queries di satu halaman, the_post() di query pertama akan menghijack global post object. Akibatnya, fungsi seperti the_title(), the_ID(), get_the_ID() akan mengembalikan data dari query terakhir, bukan dari halaman yang sedang dilihat.
Solusi
Panggil wp_reset_postdata() setelah setiap custom query selesai:
// Query 1: Related Professors
while ($relatedProfessors->have_posts()) { ... }
wp_reset_postdata(); // ← RESET setelah query 1
// Query 2: Related Events
while ($homepageEvents->have_posts()) { ... }
wp_reset_postdata(); // ← RESET setelah query 2Visualisasi
┌─ Halaman: Biology (ID: 97) ─────────────────────┐
│ │
│ get_the_ID() → 97 ✅ │
│ │
│ ┌─ Query 1: Professors ───────────────┐ │
│ │ the_post() → set global to ID: 104 │ │
│ │ get_the_ID() → 104 │ │
│ └─────────────────────────────────────┘ │
│ │
│ wp_reset_postdata() → reset ke ID: 97 ✅ │
│ │
│ ┌─ Query 2: Events ──────────────────┐ │
│ │ get_the_ID() → 97 ✅ (benar!) │ │
│ │ Meta query LIKE "97" berfungsi │ │
│ └────────────────────────────────────┘ │
└────────────────────────────────────────────────────┘⚠️ Aturan: Selalu panggil
wp_reset_postdata()setelah setiap custom query loop selesai, terutama jika ada query lain setelahnya yang bergantung padaget_the_ID()atau fungsi global lainnya.