WordPress自定义搜索结果,默认搜索显示所有,比如:文章,产品,项目,页面等。特殊情况下需要自定义搜索结果,只显示产品或项目。
一、Divi自定义搜索结果:
// Divi 搜索模块中的产品和其他帖子类型
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();
/* 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'] );
}
}
}
$postTypes[] = ‘project’;,修改project,自定义类型,比如产品:product
添加搜索多个类型:
/* BEGIN Add custom post types */ $postTypes[] = 'product'; $postTypes[] = 'project'; /* END Add custom post types */

WordPress Divi自定义搜索类型,产品,文章,项目