I’m implementing some particular search choices that ought to exclude something not of the kind put up
. The Occasions Calendar is interfering and modifying the question on the fly anyway.
My question mod simply seems to be like:
$query->post_type
And I’m doing this earlier than the question in query:
remove_action(
'pre_get_posts',
[ 'Tribe__Events__Query', 'pre_get_posts' ],
50
);
remove_action(
'parse_query',
[ 'Tribe__Events__Query', 'parse_query' ],
50
);
…and restoring the actions afterward to renew regular operation.
So with this debug code:
add_filter('question', operate($sql) {
world $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;
});
…I can see that the post_type on the question object itself is getting set accurately, and the tax_query
is on the default setting (no occasions calendar stuff). However by the point WordPress’s question
filter runs, the precise SQL that runs is:
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 ('put up', 'web 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
Edit 1
Here is the total WP_Query
object dump:
WP_Query::__set_state(array(
'question' =>
array (
's' => 'persian',
),
'query_vars' =>
array (
's' => 'persian',
'error' => '',
'm' => '',
'p' => 0,
'post_parent' => '',
'subpost' => '',
'subpost_id' => '',
'attachment' => '',
'attachment_id' => 0,
'title' => '',
'static' => '',
'pagename' => '',
'page_id' => 0,
'second' => '',
'minute' => '',
'hour' => '',
'day' => 0,
'monthnum' => 0,
'yr' => 0,
'w' => 0,
'category_name' => '',
'tag' => '',
'cat' => '',
'tag_id' => '',
'writer' => '',
'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 => 'the place',
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' => 'put up',
'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 ('put up', 'web 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,
))
From this I see that the additional tribe-* clauses are already in there. Not completely positive the place they’re coming from or easy methods to eliminate them.
Edit 2
To provide a bit extra element, I am attempting to implement a dropdown that offers customers the flexibility to specify what I am calling a search kind, which embody:
- All – default search habits
- Weblog posts – kind
put up
solely - Courses – posts of kind
tribe_events
within thelessons
class - Membership Occasions – posts of kind
tribe_events
within theclub-events
class
Here is a top level view of what I’ve to date:
In capabilities.php
add_action( 'pre_get_posts', operate( WP_Query $question ) {
$searchType = isset($_GET['search_type']) ? $_GET['search_type'] : '';
SearchFilterAbstractBase::filter_results_by_search_type($question, $searchType);
});
SearchFilter/AbstractBase.php
namespace SearchFilter;
use WP_Query;
use Timber;
summary class AbstractBase {
/**
* Supported search_types and the particular class chargeable for modifying the question for every kind
*/
protected static $SUPPORTED_TYPES = [
'post' => 'wacSearchFilterPostFilter',
'tribe_events_category_classes' => 'wacSearchFilterClassFilter',
'tribe_events_category_club_events' => 'wacSearchFilterClubEventFilter',
];
/**
* The occasion of SearchFilterAbstractBase to tug queries from
*/
protected static $filteredSearchQuery;
/**
* Modify $question to restrict search outcomes to the required $kind.
* Supported varieties are enumerated within the $SUPPORTED_TYPES property.
*/
public static operate filter_results_by_search_type(WP_Query $question, $kind = '') {
// solely have an effect on basic search queries
if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
// work out what kind of search that is
if (isset(static::$SUPPORTED_TYPES[$type])) {
$class = static::$SUPPORTED_TYPES[$type];
$filter = new $class($question);
} else {
$filter = new DefaultFilter($question);
}
$filter->modify_query();
static::$filteredSearchQuery = $filter;
}
}
/**
* Constructor
* @param WP_Query $question the WP_Query object to switch
*/
protected operate __construct(WP_Query $question) {
$this->question = $question;
}
/**
* Returns the modified question
*/
summary public operate modify_query();
public operate get_posts() {
$this->override_tribe_hooks();
$posts = Timber::get_posts();
$this->restore_tribe_hooks();
return $posts;
}
protected operate 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',
'worth' => date('Y-m-d h:i:s'),
'examine' => '>=',
'kind' => 'DATE'
),
array(
'key' => '_EventEndDate',
'worth' => date('Y-m-d h:i:s'),
'examine' => '>',
'kind' => 'DATE'
),
array(
'relation' => 'AND',
array(
'key' => '_EventStartDate',
'examine' => 'NOT EXISTS',
'kind' => 'DATE'
),
array(
'key' => '_EventEndDate',
'examine' => 'NOT EXISTS',
'kind' => 'DATE'
)
)
));
}
/**
* Take away default The Occasions Calendar hook for querying occasions
*/
protected operate 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 operate 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
);
}
SearchFilter/DefaultFilter.php
namespace SearchFilter;
use Timber;
class DefaultFilter extends AbstractBase {
public operate modify_query() {
$this->hide_recurring_events();
}
public operate get_posts() {
return Timber::get_posts();
}
}
SearchFilter/PostFilter.php
namespace SearchFilter;
use WP_Query;
class PostFilter extends AbstractBase {
public operate modify_query() {
$this->query->query_vars->post_type = ['post'];
}
}
search.php
$information['posts'] = SearchFilterAbstractBase::get_filtered_query()->get_posts();
// render posts
BTW, Timber is a low-level plugin that gives a extra OO-friendly interface round get_posts
and the like. I’ve used it on tons of WP tasks and it by no means will get in the way in which like this, so I do not assume it is the difficulty right here.
What am I lacking?