Please ensure Javascript is enabled for purposes of website accessibility

Divi: Add Projects to Search Results

Did you know that by default, the WordPress search function does not include the Divi Projects content within your results?

In fact, all custom post types are excluded by default — nothing personal against Divi.

Now I have a client website, Overhead Door Company of the 7 Rivers Region, where all of their products are entered as Projects within Divi. Because they are Projects, a custom post type, all products were excluded from the search results. Of course this created an issue, so I went about finding the solution and am sharing it below.

All you need to do is copy and paste the code below into your child theme’s functions.php file and you should be all set.

Hope this saves you time!

[php wraplines=”true”]

// ADD DIVI PROJECTS TO SEARCH RESULTS

function my_remove_default_et_pb_custom_search() {
remove_action( ‘pre_get_posts’, ‘et_pb_custom_search’ );
add_action( ‘pre_get_posts’, ‘my_et_pb_custom_search’ );
}
add_action( ‘wp_loaded’, ‘my_remove_default_et_pb_custom_search’ );

function my_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[] = ‘project’;
/* 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’] );
}
}
}

[/php]

Credit: Got the source code from the ET forum, but it was not ready to go out of the box. Needed to change the post type to “project” to get this working.