Skip to content

Post Types & Taxonomies

This guide explains how to create and manage custom post types and taxonomies in LamaPress.

Table of Contents


Overview

LamaPress provides a simple configuration-based approach to registering custom post types and taxonomies. Define them in configuration files, and they're automatically registered with WordPress.

Custom Post Types

Configuration

Post types are configured in config/post-types.php:

php
<?php

return [
    'article' => [
        'name' => 'Articles',
        'singular_name' => 'Article',
        'menu_icon' => 'dashicons-media-document',
        'template' => 'components/templates/flex.php',
        'posts_per_page' => 9,
    ],
];

Post Type Options

Required:

  • name (string) - Plural display name
  • singular_name (string) - Singular display name

Optional:

  • menu_icon (string) - Dashicon class (e.g., 'dashicons-media-document')
  • template (string) - Default template path (e.g., 'components/templates/flex.php')
  • posts_per_page (int) - Number of posts per archive page
  • relations (array) - Post type relationships (see below)

Example:

php
'article' => [
    'name' => 'Articles',
    'singular_name' => 'Article',
    'menu_icon' => 'dashicons-media-document',
    'template' => 'components/templates/flex.php',
    'posts_per_page' => 9,
    'relations' => [
        [
            'post_type' => 'employee',
        ],
    ],
],

Template Assignment

Assign a default template to a post type:

php
'article' => [
    'template' => 'components/templates/flex.php',
],

This template is used when no specific template is selected in the WordPress admin.

Relations

Define relationships between post types:

php
'article' => [
    'relations' => [
        [
            'post_type' => 'employee',
        ],
    ],
],

Relations enable bidirectional relationships between post types (e.g., articles can be related to employees).

Custom Taxonomies

Configuration

Taxonomies are configured in config/taxonomies.php:

php
<?php

return [
    'award' => [
        'post_types' => 'developer',
        'name' => 'Awards',
        'singular_name' => 'Award',
    ],
    'skill' => [
        'post_types' => ['developer', 'article'],
        'name' => 'Skills',
        'singular_name' => 'Skill',
    ],
];

Taxonomy Options

Required:

  • post_types (string|array) - Post type(s) to attach taxonomy to

Optional:

  • name (string) - Plural display name
  • singular_name (string) - Singular display name

Example:

php
'category' => [
    'post_types' => ['article', 'post'],
    'name' => 'Categories',
    'singular_name' => 'Category',
],

Working with Post Types

Querying Posts

Use standard WordPress query functions:

php
<?php
// Query articles
$articles = get_posts([
    'post_type' => 'article',
    'posts_per_page' => 10,
]);

// WP_Query
$query = new WP_Query([
    'post_type' => 'article',
    'posts_per_page' => 9,
]);
?>

Template Files

Create template files following WordPress hierarchy:

  • Archive: archive-{post_type}.php (e.g., archive-article.php)
  • Single: single-{post_type}.php (e.g., single-article.php)

Or use templates from components/templates/ (see Templates System).

ACF Fields

Create ACF fields for post types:

  1. Component Fields: Add acf.php to section/block components
  2. Global Fields: Create config/fields/{post_type}.php for post type-specific fields

Example config/fields/article.php:

php
<?php

return [
    'key' => 'article_fields',
    'title' => 'Article Fields',
    'type' => ['post_type' => 'article'],
    'fields' => [
        // ... field definitions
    ],
];

Working with Taxonomies

Getting Terms

Use standard WordPress functions:

php
<?php
// Get all terms
$terms = get_terms([
    'taxonomy' => 'award',
    'hide_empty' => false,
]);

// Get terms for a post
$post_terms = wp_get_post_terms($post_id, 'award');
?>

Term Templates

Create term template files:

  • Term Archive: taxonomy-{taxonomy}.php (e.g., taxonomy-award.php)
  • Specific Term: taxonomy-{taxonomy}-{term-slug}.php

Best Practices

1. Naming Conventions

  • Use lowercase, singular names for post type keys (e.g., 'article', not 'articles')
  • Use descriptive, plural names for display (e.g., 'Articles')
  • Follow WordPress naming conventions

2. Template Organization

  • Assign default templates to post types
  • Use consistent template naming
  • Group related templates together

3. ACF Integration

  • Create global fields for post type-specific data
  • Use component fields for reusable content
  • Keep field keys unique and descriptive

Next Steps:

Released under the MIT License.