Last updated on October 18, 2023

Divi custom post types

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Create custom post types in Divi with code.

Divi is a popular WordPress theme that allows you to create custom post types easily. Custom post types are a powerful feature in WordPress that allow you to create different types of content beyond just posts and pages. With Divi, you can create custom post types directly from the Divi Builder interface.

To create a custom post type in Divi, you can use the et_builder_post_types filter. This filter allows you to add or modify the post types that Divi recognizes. You can use this filter in your theme’s functions.php file or in a custom plugin.

Here’s an example of how you can use the et_builder_post_types filter to add a custom post type called “Books”:

function wpsnippets_add_custom_post_type( $post_types ) {
    $post_types['book'] = array(
        'slug'       => 'books',
        'menu_icon'  => 'dashicons-book',
        'labels'     => array(
            'name'          => __( 'Books', 'text-domain' ),
            'singular_name' => __( 'Book', 'text-domain' ),
        ),
    );

    return $post_types;
}
add_filter( 'et_builder_post_types', 'wpsnippets_add_custom_post_type' );

In this example, we define a new post type called “Books” with the slug “books”. We also specify a menu icon using the dashicons-book class from the WordPress Dashicons library. Finally, we provide labels for the post type, including the plural and singular names.

By adding this code to your theme’s functions.php file or a custom plugin, you will be able to create and manage “Books” as a separate post type in the Divi Builder interface.

This code snippet can be useful when you want to extend the functionality of Divi by creating custom post types for different types of content on your website. For example, if you have a website about movies, you can create a custom post type for movies and have a dedicated interface in the Divi Builder to manage movie-related content.

Examples

Example 1: Creating a Divi custom post type

This example demonstrates how to create a custom post type in Divi using the register_post_type() function.

function wpsnippets_create_custom_post_type() {
    $args = array(
        'public' => true,
        'label'  => 'Custom Post Type',
        'supports' => array( 'title', 'editor', 'thumbnail' ),
    );
    register_post_type( 'custom_post_type', $args );
}
add_action( 'init', 'wpsnippets_create_custom_post_type' );

In this code example, we define a function wpsnippets_create_custom_post_type() that is hooked to the init action. Inside the function, we use the register_post_type() function to create a custom post type called “Custom Post Type”. We set the public parameter to true to make the post type publicly accessible. We also specify the supported features for this post type, including the title, editor, and thumbnail.

Example 2: Adding custom fields to a Divi custom post type

This example demonstrates how to add custom fields to a Divi custom post type using the add_meta_box() function.

function wpsnippets_add_custom_fields() {
    add_meta_box(
        'custom_fields',
        'Custom Fields',
        'wpsnippets_render_custom_fields',
        'custom_post_type',
        'normal',
        'default'
    );
}
add_action( 'add_meta_boxes', 'wpsnippets_add_custom_fields' );

function wpsnippets_render_custom_fields() {
    // Render custom fields here
}

In this code example, we define a function wpsnippets_add_custom_fields() that is hooked to the add_meta_boxes action. Inside the function, we use the add_meta_box() function to add a meta box called “Custom Fields” to the custom post type “customposttype”. We also specify the callback function wpsnippets_render_custom_fields() that will be used to render the custom fields within the meta box.

Example 3: Modifying the Divi custom post type query

This example demonstrates how to modify the query for a Divi custom post type using the pre_get_posts action.

function wpsnippets_modify_custom_post_type_query( $query ) {
    if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'custom_post_type' ) ) {
        $query->set( 'posts_per_page', 10 );
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'wpsnippets_modify_custom_post_type_query' );

In this code example, we define a function wpsnippets_modify_custom_post_type_query() that is hooked to the pre_get_posts action. Inside the function, we check if the query is the main query for the custom post type archive “customposttype”. If it is, we modify the query parameters to set the number of posts per page to 10, order the posts by title in ascending order.

Last updated on October 18, 2023. Originally posted on December 13, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *