Creating Relationships Between Content
Tujuan
- Membuat custom post type baru: Program (contoh: Math, Biology, English)
- Membuat relasi antara Event dan Program menggunakan ACF Relationship field
- Setup archive dan single template untuk Programs
Langkah 1: Mendaftarkan Post Type "Program"
Buka wp-content/mu-plugins/university-post-types.php, tambahkan register_post_type baru di bawah event:
php
<?php
function university_post_types() {
// Event post type
register_post_type('event', array(
'show_in_rest' => true,
'supports' => array('title', 'editor', 'excerpt'),
'has_archive' => true,
'rewrite' => array('slug' => 'events'),
'public' => true,
'labels' => array(
'name' => 'Events',
'add_new_item' => 'Add New Event',
'edit_item' => 'Edit Event',
'all_items' => 'All Events',
'singular_name' => 'Event'
),
'menu_icon' => 'dashicons-calendar'
));
// Program post type
register_post_type('program', array(
'show_in_rest' => true,
'supports' => array('title', 'editor'),
'has_archive' => true,
'rewrite' => array('slug' => 'programs'),
'public' => true,
'labels' => array(
'name' => 'Programs',
'add_new_item' => 'Add New Program',
'edit_item' => 'Edit Program',
'all_items' => 'All Programs',
'singular_name' => 'Program'
),
'menu_icon' => 'dashicons-awards'
));
}
add_action('init', 'university_post_types');| Perbedaan Program vs Event | Penjelasan |
|---|---|
Tidak ada 'excerpt' di supports | Program tidak perlu excerpt manual |
Slug: 'programs' | URL archive: /programs |
Icon: 'dashicons-awards' | Ikon piala/penghargaan |
⚠️ Setelah register post type baru → buka Settings → Permalinks → Save Changes untuk rebuild permalink.
Langkah 2: Buat Post Contoh
Di WordPress Admin → Programs → Add New:
- Math (+ dummy content)
- Biology (+ dummy content)
- English (+ dummy content)
Langkah 3: Template Single Program
Buat file: single-program.php di folder tema.
Copy isi dari single-event.php sebagai template awal, lalu modifikasi:
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="metabox metabox--position-up metabox--with-home-link">
<p>
<a class="metabox__blog-home-link" href="<?php echo get_post_type_archive_link('program'); ?>">
All Programs
</a>
<span class="metabox__main"><?php the_title(); ?></span>
</p>
</div>
<div class="generic-content">
<?php the_content(); ?>
</div>
</div>
<?php get_footer(); ?>Langkah 4: Template Archive Program
Buat file: archive-program.php di folder tema:
php
<?php get_header(); ?>
<div class="page-banner">
<div class="page-banner__content container container--narrow">
<h1 class="page-banner__title">All Programs</h1>
<div class="page-banner__intro">
<p>There is something for everyone. Have a look around.</p>
</div>
</div>
</div>
<div class="container container--narrow page-section">
<ul class="link-list min-list">
<?php
while (have_posts()) {
the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php } ?>
</ul>
<?php echo paginate_links(); ?>
</div>
<?php get_footer(); ?>Langkah 5: Urutkan Programs Alfabetis (pre_get_posts)
Di functions.php, tambahkan kondisi baru di dalam fungsi university_adjust_queries:
php
function university_adjust_queries($query) {
if (!is_admin() AND is_post_type_archive('program') AND $query->is_main_query()) {
$query->set('orderby', 'title');
$query->set('order', 'ASC');
$query->set('posts_per_page', -1);
}
if (!is_admin() AND is_post_type_archive('event') AND $query->is_main_query()) {
$today = date('Ymd');
$query->set('meta_key', 'event_date');
$query->set('orderby', 'meta_value_num');
$query->set('order', 'ASC');
$query->set('meta_query', array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $today,
'type' => 'numeric'
)
));
}
}
add_action('pre_get_posts', 'university_adjust_queries');| Parameter | Nilai | Penjelasan |
|---|---|---|
orderby | 'title' | Urutkan berdasarkan judul (abjad) |
order | 'ASC' | A → Z |
posts_per_page | -1 | Tampilkan semua program di satu halaman |
Langkah 6: Membuat Relationship Field (ACF)
Buat Field Group Baru
- WordPress Admin → Custom Fields → Add New
- Nama: Related Program(s)
- Klik Add Field:
| Setting | Nilai |
|---|---|
| Field Label | Related Program(s) |
| Field Name | related_programs |
| Field Type | Relationship (di bawah kategori "Relational") |
| Filter by Post Type | Program |
| Filters | Hanya Search (hapus Post Type & Taxonomy) |
| Return Format | Post Object (default) |
- Location: Show this field group if Post Type = Event
- Klik Publish
Menggunakan Relationship Field
Edit event post (contoh: Science of Cats):
- Scroll ke bawah → field Related Programs
- Kolom kiri: daftar semua program
- Klik program → pindah ke kolom kanan (terpilih)
- Contoh: pilih Biology
💡 Relationship field mengembalikan array of post objects — bisa berisi satu atau banyak relasi.