Skip to main content
All CollectionsTroubleshooting and FAQsFrequently Asked QuestionsWooCommerce
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

Learn how to modify your theme's functions, and use custom code to include products and various post types in your search results.

Updated over 4 months ago

Enhancing the search functionality on your Divi-powered website can significantly improve user experience and help visitors find the content they are looking for more efficiently.

In this article, we will guide you through the process of enabling products and other custom post types in Divi's Search Module.

The Divi Search Module works out of the box only with the standard post types (Post and Page). To enable other post types, like products, you will need to add custom PHP code. 

Enable Products and Other Post Types in Divi's Search Module using a Child theme

  1. Install and Activate a Child theme for Divi

  2. Go to WordPress Dashboard → Appearance → Theme File Editor

  3. Select the functions.php file from the child theme

  4. Add this PHP code after the existing code

    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'] );
    }
    }
    }

Enable Products and Other Post Types in Divi's Search Module using the Code Snippets plugin

  1. Install and activate the Code Snippets plugin

  2. Go to WordPress Dashboard → Snippets → Add new and create a new PHP snippet

  3. Give the snippet a name and copy/paste the following code:

    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'] );
    }
    }
    }

  4. Save the snippet.

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

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

For example, to include the project custom post type, the code will look like this:

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

Pro 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 the backend when you move your mouse over the View button:

WordPress - Find the slug of a custom post type
Did this answer your question?