{"id":12057707,"date":"2022-09-15T09:49:44","date_gmt":"2022-09-15T09:49:44","guid":{"rendered":"https:\/\/wpml.org\/?post_type=faq&#038;p=12057707"},"modified":"2023-09-06T09:31:29","modified_gmt":"2023-09-06T09:31:29","slug":"how-to-get-other-languages-of-current-post","status":"publish","type":"faq","link":"https:\/\/wpml.org\/ru\/faq\/how-to-get-other-languages-of-current-post\/","title":{"rendered":"How can I get other languages for a current post?"},"content":{"rendered":"<p class=\"lead\">You can get other languages for a current post using several of WPML\u2019s hooks. These hooks can help you get all other languages, a specific language, and current language of a post.<\/p>\n<p>Getting other languages of a current post is a common way to build useful features for your site. For instance, you can build a custom language switcher with links to all the translations of the current post.<\/p>\n<h2>How can I get all other languages for a post?<\/h2>\n<p>You can get other languages of a post using three main hooks:<\/p>\n<ul>\n<li><a href=\"https:\/\/wpml.org\/ru\/wpml-hook\/wpml_element_type\/\">wpml_element_type<\/a>, which allows you to get the <strong>element type<\/strong> for a post type or taxonomy as saved in the database<\/li>\n<li><a href=\"https:\/\/wpml.org\/ru\/wpml-hook\/wpml_element_trid\/\">wpml_element_trid<\/a>, which allows you to get the translation group ID (<strong>trid<\/strong>) of a translated element<\/li>\n<li><a href=\"https:\/\/wpml.org\/ru\/wpml-hook\/wpml_get_element_translations\/\">wpml_get_element_translations<\/a>, which allows you to get the translations of an element using the <strong>trid<\/strong> and the <strong>element type<\/strong><\/li>\n<\/ul>\n<p>In this example, we will get the other languages of a product, however this method works for any post type.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$post_id = 47;\r\n \r\n$type = apply_filters( 'wpml_element_type', get_post_type( $post_id ) );\r\n$trid = apply_filters( 'wpml_element_trid', false, $post_id, $type );\r\n \r\n$translations = apply_filters( 'wpml_get_element_translations', array(), $trid, $type );\r\nforeach ( $translations as $lang =&gt; $translation ) {\r\n\tvar_dump( $translation );\r\n}\r\n<\/pre>\n<p>This will output something like:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nclass stdClass#4886 (8) {\r\n  public $translation_id =&gt; string(2) &quot;60&quot;\r\n  public $language_code =&gt; string(2) &quot;en&quot;\r\n  public $element_id =&gt; string(2) &quot;47&quot;\r\n  public $source_language_code =&gt; NULL\r\n  public $element_type =&gt; string(12) &quot;post_product&quot;\r\n  public $original =&gt; string(1) &quot;1&quot;\r\n  public $post_title =&gt; string(13) &quot;Example  Product&quot;\r\n  public $post_status =&gt; string(7) &quot;publish&quot;\r\n}\r\n \r\nclass stdClass#4877 (8) {\r\n  public $translation_id =&gt; string(2) &quot;61&quot;\r\n  public $language_code =&gt; string(2) &quot;fr&quot;\r\n  public $element_id =&gt; string(2) &quot;48&quot;\r\n  public $source_language_code =&gt; string(2) &quot;en&quot;\r\n  public $element_type =&gt; string(12) &quot;post_product&quot;\r\n  public $original =&gt; string(1) &quot;0&quot;\r\n  public $post_title =&gt; string(15) &quot;Exemple de produit&quot;\r\n  public $post_status =&gt; string(7) &quot;publish&quot;\r\n}\r\n<\/pre>\n<p>You can see more information about the meaning of each field in our documentation about <a href=\"https:\/\/wpml.org\/documentation\/support\/wpml-tables\/\">WPML\u2019s tables<\/a>.<\/p>\n<h2>How can I get a specific language for a post?<\/h2>\n<p>If you know the target language you want to get information for, you can convert the ID using the <a href=\"https:\/\/wpml.org\/ru\/wpml-hook\/wpml_object_id\/\">wpml_object_id<\/a> hook. For example:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ will return the post ID in the current language for post ID 1\r\necho apply_filters( 'wpml_object_id', 1, 'post' );\r\n \r\n\/\/ will return the category ID in the current language for category ID 4. If the translation is missing it will return the original (here: category ID 4)\r\necho apply_filters( 'wpml_object_id', 4, 'category', TRUE  );\r\n \r\n\/\/ will return the German attachment ID for attachment ID 25. If the translation is missing it will return NULL\r\necho apply_filters( 'wpml_object_id', 25, 'attachment', FALSE, 'de' );\r\n<\/pre>\n<p>Once you have the post ID for the specific target language, you can use it to get any information about that post.<\/p>\n<h2>How can I get the current language of a post?<\/h2>\n<p>Read more about <a href=\"https:\/\/wpml.org\/ru\/faq\/how-to-get-current-language-with-wpml\/\">how to get the current language with WPML<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can get other languages for a current post using WPML&#8217;s hooks to access all, specific, or the current language.<\/p>\n","protected":false},"author":353313,"featured_media":0,"menu_order":0,"template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_uag_custom_page_level_css":"","ep_exclude_from_search":false,"footnotes":""},"faq-category":[26137],"class_list":["post-12057707","faq","type-faq","status-publish","hentry","faq-category-how-to"],"spectra_custom_meta":{"_subscription_coupon_product_id":["0"],"_wpv_contains_gutenberg_views":[""],"_wpml_word_count":["533"],"_edit_lock":["1768392343:538393"],"_edit_last":["578773"],"_wpml_media_featured":["1"],"_wpml_media_duplicate":["1"],"_alp_processed":["1693992690"],"_wpml_location_migration_done":["1"],"_syntaxhighlighter_encoded":["1"],"_yoast_wpseo_content_score":["90"],"_yoast_wpseo_estimated-reading-time-minutes":[""],"_yoast_wpseo_wordproof_timestamp":[""],"faq_order":["10"],"wpcf-faq-category":["6"],"_views_template":["0"],"_yoast_wpseo_primary_faq-category":["26137"],"_oembed_fa81f4fedbb9b5b7aa27d2b1362e6e14":["<iframe title=\"Translating a WooCommerce product with WPML 3.4\" width=\"525\" height=\"394\" src=\"https:\/\/www.youtube.com\/embed\/4if5anyCUtE?feature=oembed&enablejsapi=1&origin=https:\/\/wpml.org\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>"],"_oembed_time_fa81f4fedbb9b5b7aa27d2b1362e6e14":["1666779083"],"_oembed_bbf56b1d4c25ce11481afba39f176f85":["<iframe title=\"Using WPML to translate content created with page builders\" width=\"700\" height=\"394\" src=\"https:\/\/www.youtube.com\/embed\/MSqNzrdPMkE?feature=oembed&enablejsapi=1&origin=https:\/\/wpml.org\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>"],"_oembed_time_bbf56b1d4c25ce11481afba39f176f85":["1698563440"],"_oembed_8631ce55de250fba0cc0768ef0e446d5":["{{unknown}}"],"_oembed_f90d7acce1be2efb8a597da9feb623da":["{{unknown}}"],"_oembed_4df63268970b52a287fd50ba6fc399cb":["{{unknown}}"],"_oembed_0a9b7c0e350cccf75658aa2dcf88ed85":["<iframe title=\"How to migrate from a custom database into WordPress\" width=\"700\" height=\"394\" src=\"https:\/\/www.youtube.com\/embed\/2PIChT5T0oQ?feature=oembed&enablejsapi=1&origin=https:\/\/wpml.org\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>"],"_oembed_time_0a9b7c0e350cccf75658aa2dcf88ed85":["1740049949"],"_uag_css_file_name":["uag-css-12057707.css"]},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"gform-image-choice-sm":false,"gform-image-choice-md":false,"gform-image-choice-lg":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false},"uagb_author_info":{"display_name":"Allison Rivers","author_link":"https:\/\/wpml.org\/ru\/author\/allison-r\/"},"uagb_comment_info":0,"uagb_excerpt":"You can get other languages for a current post using WPML's hooks to access all, specific, or the current language.","_links":{"self":[{"href":"https:\/\/wpml.org\/ru\/wp-json\/wp\/v2\/faq\/12057707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpml.org\/ru\/wp-json\/wp\/v2\/faq"}],"about":[{"href":"https:\/\/wpml.org\/ru\/wp-json\/wp\/v2\/types\/faq"}],"author":[{"embeddable":true,"href":"https:\/\/wpml.org\/ru\/wp-json\/wp\/v2\/users\/353313"}],"version-history":[{"count":5,"href":"https:\/\/wpml.org\/ru\/wp-json\/wp\/v2\/faq\/12057707\/revisions"}],"predecessor-version":[{"id":14350117,"href":"https:\/\/wpml.org\/ru\/wp-json\/wp\/v2\/faq\/12057707\/revisions\/14350117"}],"wp:attachment":[{"href":"https:\/\/wpml.org\/ru\/wp-json\/wp\/v2\/media?parent=12057707"}],"wp:term":[{"taxonomy":"faq-category","embeddable":true,"href":"https:\/\/wpml.org\/ru\/wp-json\/wp\/v2\/faq-category?post=12057707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}