Created
February 16, 2016 21:10
-
-
Save lots0logs/d6e8ff16beec201eb3ec to your computer and use it in GitHub Desktop.
WordPress :: Divi Builder :: Search Module :: Custom Post Types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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[] = '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'] ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there - No idea why but this does not work for me. I tried with post-type "project" - this works, but with any custom post type (in my case "download") it does not have an effect on the search results. Do you have any idea what could have gone wrong? Thanks for your help!