如何覆盖事件日历查询修改?

时间:2017-02-08 作者:acobster

我正在实现一些特殊的搜索选项,应该排除任何不属于该类型的内容post. 无论如何,事件日历正在干扰和动态修改查询。

我的查询模块如下所示:

$query->post_type
我在提出问题之前就这样做了:

remove_action(
  \'pre_get_posts\',
  [ \'Tribe__Events__Query\', \'pre_get_posts\' ],
  50
);

remove_action(
  \'parse_query\',
  [ \'Tribe__Events__Query\', \'parse_query\' ],
  50
);
。。。然后恢复操作以恢复正常操作。

因此,使用此调试代码:

    add_filter(\'query\', function($sql) {
        global $wp_query;
        if (isset($_GET[\'s\']) && strpos($sql, $_GET[\'s\']) !== false) {
            debug($wp_query->post_type);
            debug($wp_query->tax_query);
            debug($sql);
        }
        return $sql;
    });
。。。我可以看到查询对象本身上的post\\u类型设置正确,并且tax_query 处于默认设置(无事件日历内容)。但到WordPressquery 筛选器运行时,实际运行的SQL为:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  
WHERE 1=1  
  AND (((wp_posts.post_title LIKE \'%persian%\') OR (wp_posts.post_excerpt LIKE \'%persian%\') OR (wp_posts.post_content LIKE \'%persian%\')))  
  AND (wp_posts.post_password = \'\')  
  AND wp_posts.post_type IN (\'post\', \'page\', \'attachment\', \'tribe_venue\', \'tribe_events\', \'tribe-ea-record\', \'service\', \'facility\') 
  AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'acf-disabled\' 
  OR wp_posts.post_status = \'tribe-ea-success\' 
  OR wp_posts.post_status = \'tribe-ea-failed\' 
  OR wp_posts.post_status = \'tribe-ea-schedule\' 
  OR wp_posts.post_status = \'tribe-ea-pending\' 
  OR wp_posts.post_status = \'tribe-ea-draft\')  
ORDER BY wp_posts.post_title LIKE \'%persian%\' 
  DESC, wp_posts.post_date DESC LIMIT 0, 6
编辑1WP_Query 对象转储:

WP_Query::__set_state(array(
   \'query\' => 
  array (
    \'s\' => \'persian\',
  ),
   \'query_vars\' => 
  array (
    \'s\' => \'persian\',
    \'error\' => \'\',
    \'m\' => \'\',
    \'p\' => 0,
    \'post_parent\' => \'\',
    \'subpost\' => \'\',
    \'subpost_id\' => \'\',
    \'attachment\' => \'\',
    \'attachment_id\' => 0,
    \'name\' => \'\',
    \'static\' => \'\',
    \'pagename\' => \'\',
    \'page_id\' => 0,
    \'second\' => \'\',
    \'minute\' => \'\',
    \'hour\' => \'\',
    \'day\' => 0,
    \'monthnum\' => 0,
    \'year\' => 0,
    \'w\' => 0,
    \'category_name\' => \'\',
    \'tag\' => \'\',
    \'cat\' => \'\',
    \'tag_id\' => \'\',
    \'author\' => \'\',
    \'author_name\' => \'\',
    \'feed\' => \'\',
    \'tb\' => \'\',
    \'paged\' => 0,
    \'meta_key\' => \'\',
    \'meta_value\' => \'\',
    \'preview\' => \'\',
    \'sentence\' => \'\',
    \'title\' => \'\',
    \'fields\' => \'\',
    \'menu_order\' => \'\',
    \'embed\' => \'\',
    \'category__in\' => 
    array (
    ),
    \'category__not_in\' => 
    array (
    ),
    \'category__and\' => 
    array (
    ),
    \'post__in\' => 
    array (
    ),
    \'post__not_in\' => 
    array (
    ),
    \'post_name__in\' => 
    array (
    ),
    \'tag__in\' => 
    array (
    ),
    \'tag__not_in\' => 
    array (
    ),
    \'tag__and\' => 
    array (
    ),
    \'tag_slug__in\' => 
    array (
    ),
    \'tag_slug__and\' => 
    array (
    ),
    \'post_parent__in\' => 
    array (
    ),
    \'post_parent__not_in\' => 
    array (
    ),
    \'author__in\' => 
    array (
    ),
    \'author__not_in\' => 
    array (
    ),
    \'ignore_sticky_posts\' => false,
    \'suppress_filters\' => false,
    \'cache_results\' => true,
    \'update_post_term_cache\' => true,
    \'lazy_load_term_meta\' => true,
    \'update_post_meta_cache\' => true,
    \'post_type\' => \'any\',
    \'posts_per_page\' => 6,
    \'nopaging\' => false,
    \'comments_per_page\' => \'50\',
    \'no_found_rows\' => false,
    \'search_terms_count\' => 1,
    \'search_terms\' => 
    array (
      0 => \'persian\',
    ),
    \'search_orderby_title\' => 
    array (
      0 => \'wp_posts.post_title LIKE \\\'%persian%\\\'\',
    ),
    \'order\' => \'DESC\',
  ),
   \'tax_query\' => 
  WP_Tax_Query::__set_state(array(
     \'queries\' => 
    array (
    ),
     \'relation\' => \'AND\',
     \'table_aliases\' => 
    array (
    ),
     \'queried_terms\' => 
    array (
    ),
     \'primary_table\' => \'wp_posts\',
     \'primary_id_column\' => \'ID\',
  )),
   \'meta_query\' => 
  WP_Meta_Query::__set_state(array(
     \'queries\' => 
    array (
    ),
     \'relation\' => NULL,
     \'meta_table\' => NULL,
     \'meta_id_column\' => NULL,
     \'primary_table\' => NULL,
     \'primary_id_column\' => NULL,
     \'table_aliases\' => 
    array (
    ),
     \'clauses\' => 
    array (
    ),
     \'has_or_relation\' => false,
  )),
   \'date_query\' => false,
   \'post_count\' => 0,
   \'current_post\' => -1,
   \'in_the_loop\' => false,
   \'comment_count\' => 0,
   \'current_comment\' => -1,
   \'found_posts\' => 0,
   \'max_num_pages\' => 0,
   \'max_num_comment_pages\' => 0,
   \'is_single\' => false,
   \'is_preview\' => false,
   \'is_page\' => false,
   \'is_archive\' => false,
   \'is_date\' => false,
   \'is_year\' => false,
   \'is_month\' => false,
   \'is_day\' => false,
   \'is_time\' => false,
   \'is_author\' => false,
   \'is_category\' => false,
   \'is_tag\' => false,
   \'is_tax\' => false,
   \'is_search\' => true,
   \'is_feed\' => false,
   \'is_comment_feed\' => false,
   \'is_trackback\' => false,
   \'is_home\' => false,
   \'is_404\' => false,
   \'is_embed\' => false,
   \'is_paged\' => false,
   \'is_admin\' => false,
   \'is_attachment\' => false,
   \'is_singular\' => false,
   \'is_robots\' => false,
   \'is_posts_page\' => false,
   \'is_post_type_archive\' => false,
   \'query_vars_hash\' => \'1b23d8a973f2ad41269c66f71f8365d4\',
   \'query_vars_changed\' => false,
   \'thumbnails_cached\' => false,
   \'stopwords\' => 
  array (
    0 => \'about\',
    1 => \'an\',
    2 => \'are\',
    3 => \'as\',
    4 => \'at\',
    5 => \'be\',
    6 => \'by\',
    7 => \'com\',
    8 => \'for\',
    9 => \'from\',
    10 => \'how\',
    11 => \'in\',
    12 => \'is\',
    13 => \'it\',
    14 => \'of\',
    15 => \'on\',
    16 => \'or\',
    17 => \'that\',
    18 => \'the\',
    19 => \'this\',
    20 => \'to\',
    21 => \'was\',
    22 => \'what\',
    23 => \'when\',
    24 => \'where\',
    25 => \'who\',
    26 => \'will\',
    27 => \'with\',
    28 => \'www\',
  ),
   \'compat_fields\' => 
  array (
    0 => \'query_vars_hash\',
    1 => \'query_vars_changed\',
  ),
   \'compat_methods\' => 
  array (
    0 => \'init_query_flags\',
    1 => \'parse_tax_query\',
  ),
   \'tribe_is_event\' => false,
   \'tribe_is_multi_posttype\' => false,
   \'tribe_is_event_category\' => false,
   \'tribe_is_event_venue\' => false,
   \'tribe_is_event_organizer\' => false,
   \'tribe_is_event_query\' => false,
   \'tribe_is_past\' => false,
   \'post_type\' => \'post\',
   \'request\' => \'SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND (((wp_posts.post_title LIKE \\\'%persian%\\\') OR (wp_posts.post_excerpt LIKE \\\'%persian%\\\') OR (wp_posts.post_content LIKE \\\'%persian%\\\')))  AND (wp_posts.post_password = \\\'\\\')  AND wp_posts.post_type IN (\\\'post\\\', \\\'page\\\', \\\'attachment\\\', \\\'tribe_venue\\\', \\\'tribe_events\\\', \\\'tribe-ea-record\\\', \\\'service\\\', \\\'facility\\\') AND (wp_posts.post_status = \\\'publish\\\' OR wp_posts.post_status = \\\'acf-disabled\\\' OR wp_posts.post_status = \\\'tribe-ea-success\\\' OR wp_posts.post_status = \\\'tribe-ea-failed\\\' OR wp_posts.post_status = \\\'tribe-ea-schedule\\\' OR wp_posts.post_status = \\\'tribe-ea-pending\\\' OR wp_posts.post_status = \\\'tribe-ea-draft\\\')  ORDER BY wp_posts.post_title LIKE \\\'%persian%\\\' DESC, wp_posts.post_date DESC LIMIT 0, 6\',
   \'posts\' => NULL,
))
从这一点上,我看到额外的部落-*条款已经在那里了。不完全知道它们来自哪里,也不知道如何摆脱它们。

编辑2更详细地说,我正在尝试实现一个下拉列表,让用户能够指定我所称的search type, 其中包括:

全部-默认搜索行为博客帖子-类型post 仅限班级-职位类型tribe_eventsclasses 类别俱乐部活动-职位类型tribe_eventsclub-events 分类以下是我到目前为止的概述:

在函数中。php

add_action( \'pre_get_posts\', function( WP_Query $query ) {
  $searchType = isset($_GET[\'search_type\']) ? $_GET[\'search_type\'] : \'\';
  SearchFilter\\AbstractBase::filter_results_by_search_type($query, $searchType);
});
SearchFilter/AbstractBase。php
namespace SearchFilter;

use WP_Query;
use Timber;

abstract class AbstractBase {

  /**
   * Supported search_types and the specific class responsible for modifying the query for each type
   */
  protected static $SUPPORTED_TYPES = [
    \'post\' => \'wac\\SearchFilter\\PostFilter\',
    \'tribe_events_category_classes\' => \'wac\\SearchFilter\\ClassFilter\',
    \'tribe_events_category_club_events\' => \'wac\\SearchFilter\\ClubEventFilter\',
  ];

  /**
   * The instance of SearchFilter\\AbstractBase to pull queries from
   */
  protected static $filteredSearchQuery;

  /**
   * Modify $query to limit search results to the specified $type.
   * Supported types are enumerated in the $SUPPORTED_TYPES property.
   */
  public static function filter_results_by_search_type(WP_Query $query, $type = \'\') {
    // only affect general search queries
    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {

      // figure out what type of search this is
      if (isset(static::$SUPPORTED_TYPES[$type])) {
        $class = static::$SUPPORTED_TYPES[$type];
        $filter = new $class($query);
      } else {
        $filter = new DefaultFilter($query);
      }

      $filter->modify_query();

      static::$filteredSearchQuery = $filter;
    }
  }

  /**
   * Constructor
   * @param  WP_Query $query the WP_Query object to modify
   */
  protected function __construct(WP_Query $query) {
    $this->query = $query;
  }

  /**
   * Returns the modified query
   */
  abstract public function modify_query();

  public function get_posts() {
    $this->override_tribe_hooks();
    $posts = Timber::get_posts();
    $this->restore_tribe_hooks();

    return $posts;
  }

  protected function hide_recurring_events() {
    $this->query->set( \'tribeHideRecurrence\', true );
    $this->query->set( \'eventDisplay\', \'upcoming\' );
    $this->query->tribe_is_multi_posttype = true;
    $this->query->set(\'meta_query\', array(
      \'relation\' => \'OR\',
      array(
        \'key\' => \'_EventStartDate\',
        \'value\' => date(\'Y-m-d h:i:s\'),
        \'compare\' => \'>=\',
        \'type\' => \'DATE\'
      ),
      array(
        \'key\' => \'_EventEndDate\',
        \'value\' => date(\'Y-m-d h:i:s\'),
        \'compare\' => \'>\',
        \'type\' => \'DATE\'
      ),
      array(
        \'relation\' => \'AND\',
        array(
          \'key\' => \'_EventStartDate\',
          \'compare\' => \'NOT EXISTS\',
          \'type\' => \'DATE\'
        ),
        array(
          \'key\' => \'_EventEndDate\',
          \'compare\' => \'NOT EXISTS\',
          \'type\' => \'DATE\'
        )
      )
    ));
  }

  /**
   * Remove default The Events Calendar hook for querying events
   */
  protected function override_tribe_hooks() {
    remove_action(
      \'pre_get_posts\',
      [ \'Tribe__Events__Query\', \'pre_get_posts\' ],
      static::TRIBE_ACTION_PRIORITY
    );

    remove_action(
      \'parse_query\',
      [ \'Tribe__Events__Query\', \'parse_query\' ],
      static::TRIBE_ACTION_PRIORITY
    );
  }

  protected function restore_tribe_hooks() {
    add_action(
      \'pre_get_posts\',
      [ \'Tribe__Events__Query\', \'pre_get_posts\' ],
      static::TRIBE_ACTION_PRIORITY
    );

    add_action(
      \'parse_query\',
      [ \'Tribe__Events__Query\', \'parse_query\' ],
      static::TRIBE_ACTION_PRIORITY
    );
  }
搜索过滤器/默认过滤器。php
namespace SearchFilter;

use Timber;

class DefaultFilter extends AbstractBase {
  public function modify_query() {
    $this->hide_recurring_events();
  }

  public function get_posts() {
    return Timber::get_posts();
  }
}
搜索过滤器/后过滤器。php
namespace SearchFilter;

use WP_Query;

class PostFilter extends AbstractBase {
  public function modify_query() {
    $this->query->query_vars->post_type = [\'post\'];
  }
}
搜索。php
$data[\'posts\'] = SearchFilter\\AbstractBase::get_filtered_query()->get_posts();
// render posts
顺便说一句,Timber是一个低级插件,它在get_posts 诸如此类。我在许许多多的WP项目中使用过它,但它从来没有像这样妨碍过我,所以我认为这不是问题所在。

我错过了什么?

1 个回复
SO网友:Marc-Antoine Parent

我使用query参数完成了类似的操作supress_filters = true.

有关其工作原理的更多说明,请参阅Codex:https://codex.wordpress.org/Template_Tags/get_posts#Parameters

相关推荐

如何将变量传递给Get_Search_Form()?

是否可以将变量传递给get_search_form()? 我在两个地方使用它:一个在标题中,另一个在内容的搜索页面上。后者必须具有附加类,例如。search--full. 我试着用is_search() 但尽管它在其他页面上运行良好,但在搜索页面上,两种表单都有search--full 班