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)

Share Improve this question asked Jan 22, 2020 at 15:39 FooBarFooBar 4441 gold badge8 silver badges23 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Looking 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