Skip to content

Professors Post Type

Tujuan

  1. Membuat custom post type Professor
  2. Setup template single professor
  3. Menambahkan relasi professor ↔ program
  4. Menampilkan related professors di halaman program
  5. Mengenal wp_reset_postdata()

📝 Catatan: Saat membuat Professor post type, pastikan menyertakan 'show_in_rest' => true agar 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():

php
// 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

FiturEventProgramProfessor
has_archive❌ (tidak ada archive)
rewrite'events''programs'Tidak perlu
supportstitle, editor, excerpttitle, editortitle, editor, thumbnail
Icondashicons-calendardashicons-awardsdashicons-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

  1. Dr. Meowsalot (foto kucing)
  2. 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
<?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(); ?>

Update ACF Field Group Location

  1. Custom Fields → Edit Related Program(s) field group
  2. Di bagian Location, klik or
  3. Tambahkan: Post Type = Professor
  4. Sekarang field muncul di Event dan Professor

Update Heading di Template

Di single-professor.php, ganti teks "Related Program(s)" menjadi "Subject(s) Taught":

php
echo '<h2 class="headline headline--medium">Subject(s) Taught</h2>';

Di single-program.php, tambahkan custom query di atas kode existing related events:

php
<?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:

php
// 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 2

Visualisasi

┌─ 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 pada get_the_ID() atau fungsi global lainnya.