Last active
December 15, 2025 11:07
-
-
Save twentyfortysix/d0fac66bc6278f1de10cc8006fb260db to your computer and use it in GitHub Desktop.
WP WPML address content by language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Update ACF relation field "things" for all event posts based on WPML language | |
| * Sets 6526 for English posts and 6240 for Czech posts | |
| * | |
| * @param string $lang_code Language code ('en', 'cs', or 'any' for all languages) | |
| */ | |
| function helper_update_events_things_field($lang_code = 'any') { | |
| // Store the current language to restore later | |
| $current_lang = apply_filters('wpml_current_language', null); | |
| $languages_to_process = ($lang_code === 'any') ? ['en', 'cs'] : [$lang_code]; | |
| $total_updated = 0; | |
| $total_skipped = 0; | |
| foreach ($languages_to_process as $language) { | |
| // Switch to the specific language context | |
| do_action('wpml_switch_language', $language); | |
| // Get all event posts in this language | |
| $events = get_posts([ | |
| 'post_type' => 'event', | |
| 'post_status' => 'any', | |
| 'posts_per_page' => -1, | |
| 'fields' => 'ids', | |
| 'suppress_filters' => false | |
| ]); | |
| if (empty($events)) { | |
| echo "No event posts found for language '{$language}'.\n"; | |
| continue; | |
| } | |
| echo "Found " . count($events) . " event posts for language '{$language}'. Updating 'things' field...\n"; | |
| // Set the appropriate ID based on language | |
| $things_id = ($language === 'en') ? 6526 : 6240; | |
| foreach ($events as $event_id) { | |
| // Update the ACF field with array of IDs | |
| $updated = update_field('things', [$things_id], $event_id); | |
| if ($updated) { | |
| echo " - Post ID {$event_id} ({$language}): Set 'things' to [{$things_id}]\n"; | |
| $total_updated++; | |
| } else { | |
| echo " - Post ID {$event_id} ({$language}): Failed to update or no change needed.\n"; | |
| $total_skipped++; | |
| } | |
| } | |
| } | |
| // Restore the original language | |
| if ($current_lang) { | |
| do_action('wpml_switch_language', $current_lang); | |
| } | |
| echo "\nCompleted: {$total_updated} updated, {$total_skipped} skipped.\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment