If you are using the Divi theme, you may notice that the search module does not search WooCommerce products. It will only search the standard posts and pages.

To include product search, insert the following code to the bottom on your /functions.php file in your child theme.

You can also use a plugin such as “Download
Code Snippets” to inject the code into your website.

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

7 Comments

  1. Douglas

    Hi, I had no luck with this. Followed the instructions verbatim (I believe). Any help, REALLY appreciated.

    Thank you!

    Reply
    • Rob

      Hmm…I’m using this via code snippets and seems to work fine. Anymore technical detail you can provide?

      Reply
  2. Mike

    This breaks the page and gives a critical error.

    Reply
    • Rob

      Hmm…I’m using this via code snippets and seems to work fine. Anymore technical detail you can provide?

      Reply
  3. Rob

    Hmm…I’m using this via code snippets and seems to work fine. Anymore technical detail you can provide?

    Reply
  4. Marie-D

    You need to change the “>” in “>” line 8, 31, 35, 39 and the “&&” in “&&” line 15 , this will work.

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

    Reply
  5. Arthur Smith

    Tried this today for the first time (after fixing the object pointer code and &&), and it did not work. Is it supposed to still work on WP 6.2?

    Reply

Submit a Comment

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

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news, updates, freebies and discounts.

You have Successfully Subscribed!