enable-product-search-Divi.php
· 1.4 KiB · PHP
Raw
// from: https://help.elegantthemes.com/en/articles/3030197-how-to-enable-products-and-other-post-types-in-divi-s-search-module
// for easycut.es
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'] );
}
}
}
| 1 | // from: https://help.elegantthemes.com/en/articles/3030197-how-to-enable-products-and-other-post-types-in-divi-s-search-module |
| 2 | // for easycut.es |
| 3 | |
| 4 | function custom_remove_default_et_pb_custom_search() { |
| 5 | remove_action( 'pre_get_posts', 'et_pb_custom_search' ); |
| 6 | add_action( 'pre_get_posts', 'custom_et_pb_custom_search' ); |
| 7 | } |
| 8 | add_action( 'wp_loaded', 'custom_remove_default_et_pb_custom_search' ); |
| 9 | |
| 10 | function custom_et_pb_custom_search( $query = false ) { |
| 11 | if ( is_admin() || ! is_a( $query, 'WP_Query' ) || ! $query->is_search ) { |
| 12 | return; |
| 13 | } |
| 14 | |
| 15 | if ( isset( $_GET['et_pb_searchform_submit'] ) ) { |
| 16 | $postTypes = array(); |
| 17 | |
| 18 | if ( ! isset($_GET['et_pb_include_posts'] ) && ! isset( $_GET['et_pb_include_pages'] ) ) { |
| 19 | $postTypes = array( 'post' ); |
| 20 | } |
| 21 | |
| 22 | if ( isset( $_GET['et_pb_include_pages'] ) ) { |
| 23 | $postTypes = array( 'page' ); |
| 24 | } |
| 25 | |
| 26 | if ( isset( $_GET['et_pb_include_posts'] ) ) { |
| 27 | $postTypes[] = 'post'; |
| 28 | } |
| 29 | |
| 30 | /* BEGIN Add custom post types */ |
| 31 | $postTypes[] = 'product'; |
| 32 | /* END Add custom post types */ |
| 33 | |
| 34 | $query->set( 'post_type', $postTypes ); |
| 35 | |
| 36 | if ( ! empty( $_GET['et_pb_search_cat'] ) ) { |
| 37 | $categories_array = explode( ',', $_GET['et_pb_search_cat'] ); |
| 38 | $query->set( 'category__not_in', $categories_array ); |
| 39 | } |
| 40 | |
| 41 | if ( isset( $_GET['et-posts-count'] ) ) { |
| 42 | $query->set( 'posts_per_page', (int) $_GET['et-posts-count'] ); |
| 43 | } |
| 44 | } |
| 45 | } |