This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This thread is resolved. Here is a description of the problem and solution.

Problem:

Creating an advanced meta_query with suppress_filters

Solution:

Due to our developers' decision, the suggestion that the user gave here is not going to be implemented in WPML's code. According to you understanding and judgment, if you think that the code here below can work for you, you can use it. But please understand that we can't be responsible for this code.

This topic contains 3 replies, has 2 voices.

Last updated by Itamar 2 months, 3 weeks ago.

Assigned support staff: Itamar.

Author Posts
August 13, 2018 at 4:55 pm #2640651

debraP

I needed to find posts from all languages that had a certain meta field value, created with ACF.

This normally wouldn't be an issue, however I the meta_value I needed was stored in a repeater, and querying for that REQUIRES the use of the posts_where filter (as specified by ACF).

To get around this, I added a posts_where filter to functions.php and modified my wp_query.

<ul>
<li>The first bit checks for a new argument in the wp_query, 'wildcard_on_key'. If it's there it will continue, if not it returns.</li>
<li>The second bit updates the meta_query bit as specified by ACF</li>
<li>The third bit checks for a new argument in the wp_query, 'suppress_filters_manually'. If it's set to true, it will replace the portion of the query set by WPML that specifies which language to filter the posts by.</li>
</ul>

<h2>New Posts Where Filter</h2>
<pre>
function add_cond_to_where( $where, \WP_query $q ) {
	
  // Check for wildcard
  if( true !== $q->get('wildcard_on_key') )
    return $where;
	
  //Replace repeater_slug_% with repeater_slug_$
  $where = str_replace("meta_key = 'repeater_slug_$", "meta_key LIKE 'repeater_slug_%", $where);
  
  // Suppress WPML filter manually by replacing its search with true
  if( true === $q->get('suppress_filters_manually') ){
    $where = str_replace("t.language_code = '".ICL_LANGUAGE_CODE."' OR 0", "true", $where);
  }

  return $where;
}
add_filter('posts_where', 'add_cond_to_where', 10, 2);
</pre>

<h2>New WP Query</h2>
<pre>
$args = array(
    'post_type' => 'post',
    'meta_query' => array(
        array(
            'key' => 'repeater_slug_$_field',
            'value' => 'value',
            'compare' => '=',
        )
    ),
    'wildcard_on_key' => true,
    'suppress_filters_manually' => true,
);
$wp_query = new WP_Query($args);
</pre>

Note that in my query, the suppress_filters is not set to true. If it's set to true in this case it will prevent the posts_where filter from being applied.

I would love to see a less "hacky" version of this applied to WPML core.

August 14, 2018 at 3:46 pm #2643834

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi and thank you for this suggestion.

I'm bringing it to the attention of our second tier supporters, and they will decide how to proceed with it.

I'll keep you updated here.

Best Regards,
Itamar.

August 15, 2018 at 8:58 am #2645927

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi.

This issue is now escalated to our second tier supporters.
I'll keep you updated about it here in the thread.

Best Regards,
Itamar.

July 25, 2019 at 9:15 am #4281231

Itamar
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi.

I was going over old tickets, and I see that concerning this issue it was decided by my superiors not to implement this at the moment.

I want to thank you again for this suggestion.
I'm closing this ticket now. But is there will be any change regarding this subject I'll let you know here.

Best Regards,
Itamar.