All Collections
FAQ's and Troubleshooting
How to enable Products and other post types in Divi's Search Module
How to enable Products and other post types in Divi's Search Module
Andrei N avatar
Written by Andrei N
Updated over a week ago

The Divi Search Module works with the standard post types (post and page) by default. To enable other post types like products, you will need to add some custom PHP code. 

The first thing you will need is to create a Divi child theme to ensure that your changes persist after a Divi update. Here is a tutorial on how to create a child theme that you can follow.

Once you created the child theme, place the following code to the bottom of the /functions.php  file of your child theme:

function custom_remove_default_et_pb_custom_search() {
remove_action( 'pre_get_posts', 'et_pb_custom_search' );
add_action( 'pre_get_posts', 'custom_et_pb_custom_search' );
}
add_action( 'wp_loaded', 'custom_remove_default_et_pb_custom_search' );

function custom_et_pb_custom_search( $query = false ) {
if ( is_admin() || ! is_a( $query, 'WP_Query' ) || ! $query->is_search ) {
return;
}

if ( isset( $_GET['et_pb_searchform_submit'] ) ) {
$postTypes = array();
       
if ( ! isset($_GET['et_pb_include_posts'] ) && ! isset( $_GET['et_pb_include_pages'] ) ) {
            $postTypes = array( 'post' );
        }

if ( isset( $_GET['et_pb_include_pages'] ) ) {
            $postTypes = array( 'page' );
        }

if ( isset( $_GET['et_pb_include_posts'] ) ) {
            $postTypes[] = 'post';
        }

/* BEGIN Add custom post types */
$postTypes[] = 'product';
/* END Add custom post types */

$query->set( 'post_type', $postTypes );

if ( ! empty( $_GET['et_pb_search_cat'] ) ) {
$categories_array = explode( ',', $_GET['et_pb_search_cat'] );
$query->set( 'category__not_in', $categories_array );
}

if ( isset( $_GET['et-posts-count'] ) ) {
$query->set( 'posts_per_page', (int) $_GET['et-posts-count'] );
}
}
}

If you need to include other custom post types into the search results, you can edit this part of code:

/* BEGIN Add custom post types */
$postTypes[] = 'product';
/* END Add custom post types */

For example, to include Projects post type change the code like this:

/* BEGIN Add custom post types */
$postTypes[] = 'product';
$postTypes[] = 'project';
/* END Add custom post types */

TIP: The slug of the custom post type can usually be found in the URL when you open the one post of that type or in backend when you move your mouse over the View button:

Did this answer your question?