doufutao4428 2015-11-12 22:45
浏览 43

事件未显示在搜索结果WordPress中

I have a custom site that uses The Events Calendar to manage Events. I have a search in my main nav and I am unable to show events, only posts and pages. I have added custom filters but I know I am just overlooking something.

Here are the functions found in my theme's function.php

add_theme_support('html5' ,array('search-form'));
add_filter('wp_nav_menu_items', 'add_search_form', 10, 2);

function add_search_form($items, $args) {
          if( $args->theme_location == 'menu-top' )
          $items .= '<li class="search"><form role="search" method="get" id="searchform" action="'.home_url( '/' ).'">
          <input type="search" class="search-box" placeholder="Type Here"  name="s" />
          <input class="search-button" type="submit" id="searchsubmit" value="'. __('Search') .'" /></form>
          </li>';
     return $items;
}

//Define what post types to include in search
function include_in_search( $query ) {
        if ( $query->is_search ) {
        $query->set( 'post_type', array( 'post', 'page', 'feed', 'tribe_events' ));
        }
    return $query;
}
add_filter('the_search_query', 'include_in_search' );

I sure would appreciate a hand in this. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duandang2123 2015-11-13 00:52
    关注

    You're confusing the_search_query with pre_get_posts.

    the_search_query allows you to modify a search string before display whereas pre_get_posts allows you to modify the query being performed.

    Change this:

    add_filter('the_search_query', 'include_in_search' );
    

    To this:

    add_filter( 'pre_get_posts', 'include_in_search' );
    

    Also be sure you're only acting on the query in the right place. Add more conditionals to check you're not in the admin and you're operating on the main query.

    Change this:

    if ( $query->is_search ) {
    

    To this:

    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100