Skip to content

Instantly share code, notes, and snippets.

@tharmann
Created January 13, 2020 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tharmann/f28e4b925a2cf0a85b83b8fdb9697100 to your computer and use it in GitHub Desktop.
Save tharmann/f28e4b925a2cf0a85b83b8fdb9697100 to your computer and use it in GitHub Desktop.
Customize Divi search module to include WooCommerce products
<?php
/*Add products to divi search module. Source: https://intercom.help/elegantthemes/en/articles/3030197-how-to-enable-products-and-other-post-types-in-divi-s-search-module */
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'] );
}
}
}
/*END: Add products to divi search module*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment