AdvancedCustomFields / AdvancedCustomFields/acf

Cannot get $sitepress->switch_lang() to work properly in acf/load_field filter

Open
#259 2 comments 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

I have a multilingual website that uses WPML and I am trying to change the site language before running a WP_Query inside the acf/load_field ACF filter so that the queried results are all from a specific language regardless of what the currently viewed language is. But, what I'm seeing is slightly confusing. The goal is to dynamically populate an empty select field that's part of a custom block registered with ACF. I want this select field to have the post_id (value) and page_title (label) of all the default site language (Italian) pages regardless of what language page is being edited on the backend.

Based on other support forums I've read, if I add $sitepress->switch_lang( 'it' ) before running the WP_Query and then switch back to the current language after the query is run then that should accomplish my goal.

However, everything I've tried fails to get the select menu to populate with only Italian posts and pages. Instead, the select menu is populated with posts from the current language. The real confusing part is that if I look at the custom field's settings (Custom Fields > Field Groups) the "Choices" are dynamically populated correctly, in other words, it shows the Italian post_ids and page titles.

WPML wouldn't weigh in because they said they do not help with custom code. Perhaps y'all have an idea of what's going on or have come across this yourself. Below is the code I'm using and I've also attached two screenshots showing what the select field looks like when the custom block is selected in the block editor as well as what the select field looks like in the field's settings.

One thing that I notice is that all the ACF fields that load in the custom block show the following string below them "Field's value in original language:" This string doesn't appear if I disable the acf/load_field filter.

add_filter( 'acf/load_field/name=new_hello_split_image_link', 'query_mainsite_value_for_acf_select_field' );
 
function query_mainsite_value_for_acf_select_field( $field ) {
   
    global $sitepress;
    $current_lang = $sitepress->get_current_language(); // save current language
    $default_lang = $sitepress->get_default_language(); // save default language

    // reset choices
    $field['choices'] = array();
    $dynamic_choices = array();

    $sitepress->switch_lang( $default_lang ); // switch to default language

    // set arguments from WP_Query
    $args = array(
      'post_type' => array( 'page' ),
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'orderby' => 'post_title',
      'order' => 'ASC',
      'suppress_filters' => false
    );
    $source_posts = new WP_Query( $args ); // query the posts on the main site to populate the select field

    // loop through array and add to dynamic_choices array
    if ( $source_posts->have_posts() && ( $source_posts->found_posts > 0 ) ) {
      global $post;
      while ( $source_posts->have_posts() ) {
        $source_posts->the_post();
        $dynamic_choices[$post->ID] = $post->post_title;
      }
      wp_reset_query();
      $sitepress->switch_lang( $current_lang ); // switch back to current language
    }

    $field['choices'] = $dynamic_choices; // set the choices for the select field
    return $field; // return the field
}

choices-in-field-settings
choices-in-block-editor

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 at the acf/load_field filter and the WP_Query code shown in the issue, then reproduce the differing choices in the field settings and block editor. Done means identifying why the language change behaves differently in those contexts and documenting a confirmed fix or limitation.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.