1

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)

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' );
| improve this answer | |
  • Hi, thanks for your answer. This would then reenable the WPML filter to only fetch posts from a specific language, and not ALL languages as I need.I think you're on the right path using pre_get_posts though – FooBar Jan 22 at 16:07
0

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.