AdvancedCustomFields / AdvancedCustomFields/acf

Post_object field (and other) search compatibility with Polylang (possibly other i18n plugins)

Open
#256 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
945
Forks
197
PR merge metrics
No merged PRs in 30d

Description

Description

I have discovered a compatibility issue when using the post_object where some content do not show up in searches.

This issue could apply to all queries going through acf_field_post_object::get_ajax_query.

Polylang

Quick explanation of how Polylang works: Polylang creates a taxonomy called language. This taxonomy contains all the registered languages as terms and use these to create a "WordPress'ish" integration into \WP_Query.
So when Polylang hooks into WP_Query and finds that one of the post types that have been registered to be translated is present, then they add a filter to the tax_query argument that filters the query to only show all the results from the current language of the user.

Example:

'tax_query'      => [
	[
		'taxonomy' => 'language',
		'field'    => 'term_taxonomy_id',
		'terms'    => 802,
		'operator' => 'IN',
	],
],

What happens

So what happens in acf_field_post_object::get_ajax_query is that the Polylang logic detects that we wish to query a post type and then adds the mentioned logic.
This is not necessarily a problem if you only wish to search among content that is translated to your current language. But as soon as you do a query with post types that have not been registered as translatable then the "non translatable content" do not show up because they do not have the language term.

Example:

  • Event have not been registered as translatable
  • Post and Page is translatable

As soon as post_object includes one of post or page as the post_type argument then all events will be disregarded from the search results.

Current workaround if anybody else is in the same situation

Our client could not wait for us to find a proper solution so this is a workaround specific for at least Polylang that just disables the language taxonomy on ACF queries

/**
 * Alter the search query for ACF.
 *
 * We experienced that not all content could be found because
 */
add_filter( 'acf/fields/post_object/query', 'theme_acf_admin_search_polylang_compatibility', 10, 3 );
function theme_acf_admin_search_polylang_compatibility( $args, $field, $post_id ) {
	$args['lang'] = '';

	return $args;
}

Thanks for all your work and a happy Pro user
/ Kallehauge

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in acf_field_post_object::get_ajax_query and inspect how its WP_Query arguments interact with Polylang's language taxonomy filter. Reproduce a post_object search combining translatable Post or Page types with a non-translatable Event type, then define and verify behavior that preserves language filtering without hiding non-translatable content.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, wordpress
Domain
backend, search
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.