With over a decade of web development experience, I specialize in Drupal (7, 8, 9, 10), CodeIgniter, Laravel, and WordPress. I offer extensive expertise in both module and theme development, providing customized solutions for complex projects. Whether you need to enhance an existing platform, create new features, or seek expert guidance, I'm here to assist. My dedication to delivering high-quality, efficient, and scalable solutions is unmatched. Feel free to contact me to explore how I can contribute to your project's success. Let's turn your ideas into reality!

“Learn to create custom post types, taxonomies, and fields in WordPress programmatically, enabling dynamic content structures with ease.”

Below is an example of how you can create a custom post type with a custom taxonomy and custom fields programmatically in WordPress. This code can be added to your theme's functions.php file or a custom plugin.

<?php
// Step 1: Register Custom Post Type

function custom_post_type() {
    $labels = array(
        'name'               => _x('Books', 'post type general name'),
        'singular_name'      => _x('Book', 'post type singular name'),
        'add_new'            => _x('Add New', 'book'),
        'add_new_item'       => __('Add New Book'),
        'edit_item'          => __('Edit Book'),
        'new_item'           => __('New Book'),
        'all_items'          => __('All Books'),
        'view_item'          => __('View Book'),
        'search_items'       => __('Search Books'),
        'not_found'          => __('No books found'),
        'not_found_in_trash' => __('No books found in the Trash'),
        'parent_item_colon'  => '',
        'menu_name'          => 'Books'
    );

    $args = array(
        'labels'        => $labels,
        'public'        => true,
        'has_archive'   => true,
        'rewrite'       => array('slug' => 'books'),
        'menu_icon'     => 'dashicons-book', // You can choose an icon from Dashicons: https://developer.wordpress.org/resource/dashicons/
        'supports'      => array('title', 'editor', 'thumbnail', 'custom-fields'),
    );

    register_post_type('book', $args);
}

add_action('init', 'custom_post_type');

// Step 2: Register Custom Taxonomy

function custom_taxonomy() {
    $labels = array(
        'name'              => _x('Genres', 'taxonomy general name'),
        'singular_name'     => _x('Genre', 'taxonomy singular name'),
        'search_items'      => __('Search Genres'),
        'all_items'         => __('All Genres'),
        'parent_item'       => __('Parent Genre'),
        'parent_item_colon' => __('Parent Genre:'),
        'edit_item'         => __('Edit Genre'),
        'update_item'       => __('Update Genre'),
        'add_new_item'      => __('Add New Genre'),
        'new_item_name'     => __('New Genre Name'),
        'menu_name'         => __('Genre'),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'genre'),
    );

    register_taxonomy('genre', array('book'), $args);
}

add_action('init', 'custom_taxonomy');

// Step 3: Add Custom Fields

function custom_fields() {
    add_meta_box('book_meta_box', 'Book Details', 'display_book_meta_box', 'book', 'normal', 'high');
}

function display_book_meta_box($post) {
    // Retrieve current values of custom fields
    $author = get_post_meta($post->ID, '_author', true);
    $published_year = get_post_meta($post->ID, '_published_year', true);

    // Output fields
    ?>
    <p>
        <label for="author">Author:</label>
        <input type="text" id="author" name="author" value="<?php echo esc_attr($author); ?>">
    </p>
    <p>
        <label for="published_year">Published Year:</label>
        <input type="text" id="published_year" name="published_year" value="<?php echo esc_attr($published_year); ?>">
    </p>
    <?php
}

function save_book_meta_box($post_id) {
    // Save custom field values
    if (isset($_POST['author'])) {
        update_post_meta($post_id, '_author', sanitize_text_field($_POST['author']));
    }

    if (isset($_POST['published_year'])) {
        update_post_meta($post_id, '_published_year', sanitize_text_field($_POST['published_year']));
    }
}

add_action('add_meta_boxes', 'custom_fields');
add_action('save_post', 'save_book_meta_box');

?>

Explanation:

  1. Custom Post Type Registration (custom_post_type function):
    • Registers a custom post type named "Book" with custom labels and options.
    • Supports various features such as title, editor, thumbnail, and custom fields.
  2. Custom Taxonomy Registration (custom_taxonomy function):
    • Registers a custom taxonomy named "Genre" associated with the "Book" post type.
    • Provides hierarchical taxonomy structure with custom labels.
  3. Custom Fields (custom_fields function):
    • Adds a meta box for custom fields (Author and Published Year) on the "Book" post type editor.
    • Defines fields and their HTML markup.
  4. Display and Save Custom Fields (display_book_meta_box and save_book_meta_box functions):
    • Displays the custom fields on the post editor.
    • Saves the custom field values when the post is saved.

This code creates a custom post type "Book" with a custom taxonomy "Genre" and adds custom fields (Author and Published Year). Adjust the field names, post type, and taxonomy details according to your specific requirements.

Posted by Sujan Shrestha
Categorized:
PREVIOUS POST
banner