admin管理员组文章数量:1122826
Apparently, for some illogical reason, the developers decided that the only way to fetch posts in all languages, is to add supress_filters=true
to the WP_Query (instead of having like say, a language_code=all
option).
Anyways, I am in a situation where I need to fetch posts in all languages, but ALSO modify the WP_Query using filters. Is there a way to enforce that my filters are being added to the query, even though supress_filters
is set to false
?
This is the filter I need to add:
add_filter( 'posts_where', function($where, $wp_query) {
global $wpdb;
if($search_term = $wp_query->get( 'custom_search' )){
$search_term = $wpdb->esc_like($search_term);
$search_term = ' \'%' . $search_term . '%\'';
$where .= ' AND (' . $wpdb->posts . '.post_title LIKE ' . $search_term . ' OR ' . $wpdb->posts . '.ID LIKE ' . $search_term . ' OR ' . $wpdb->posts . '.post_name LIKE ' . $search_term . ')';
}
return $where;
}, 10, 2 );
But it is removed after adding supress_filters=true
(because I need to fetch posts in ALL languages)
Apparently, for some illogical reason, the developers decided that the only way to fetch posts in all languages, is to add supress_filters=true
to the WP_Query (instead of having like say, a language_code=all
option).
Anyways, I am in a situation where I need to fetch posts in all languages, but ALSO modify the WP_Query using filters. Is there a way to enforce that my filters are being added to the query, even though supress_filters
is set to false
?
This is the filter I need to add:
add_filter( 'posts_where', function($where, $wp_query) {
global $wpdb;
if($search_term = $wp_query->get( 'custom_search' )){
$search_term = $wpdb->esc_like($search_term);
$search_term = ' \'%' . $search_term . '%\'';
$where .= ' AND (' . $wpdb->posts . '.post_title LIKE ' . $search_term . ' OR ' . $wpdb->posts . '.ID LIKE ' . $search_term . ' OR ' . $wpdb->posts . '.post_name LIKE ' . $search_term . ')';
}
return $where;
}, 10, 2 );
But it is removed after adding supress_filters=true
(because I need to fetch posts in ALL languages)
2 Answers
Reset to default 0Looking at the WP_Query Class you should be able to use pre_get_posts
to modify the query before any other filters are run, which includes turning off the suppress_filters
flag. An example of this could be:
/**
* Modify WP_Queries
* @link https://wordpress.stackexchange.com/q/356950/7355
*
* @param WP_Query $query
*
* @return void
*/
function wpse356950_query_modifications( $query ) {
// Don't run on admin
if( is_admin() ) {
return;
}
// Some conditional to tell our query from other queries
if( $query->get( 'suppress_filters' ) ) {
$query->set( 'suppress_filters', false );
}
}
add_action( 'pre_get_posts', 'wpse356950_query_modifications' );
One way I have found to be working is to remove the filters manually added by WPML core, like so:
global $wpml_query_filter;
/**
* WPML fetch in all languages
*/
remove_filter( 'posts_join', array( $wpml_query_filter, 'posts_join_filter' ), 10, 2 );
remove_filter( 'posts_where', array( $wpml_query_filter, 'posts_where_filter' ), 10, 2 );
That's OK in my scenario, as this specific page only has one WP_Query. If you have multiple, where you need to fetch for a specific language, I recommend you readd the filters after WP_Query has run.
本文标签: filtersBypass quotsupressfiltersquot in WP Query
版权声明:本文标题:filters - Bypass "supress_filters" in WP Query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281161a1926231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论