{"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\/it\/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\/it\/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\/it\/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\/it\/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\/it\/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\/it\/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,"ep_exclude_from_search":false,"footnotes":""},"faq-category":[26137],"class_list":["post-12057707","faq","type-faq","status-publish","hentry","faq-category-how-to"],"_links":{"self":[{"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/faq\/12057707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/faq"}],"about":[{"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/types\/faq"}],"author":[{"embeddable":true,"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/users\/353313"}],"version-history":[{"count":5,"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/faq\/12057707\/revisions"}],"predecessor-version":[{"id":14350117,"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/faq\/12057707\/revisions\/14350117"}],"wp:attachment":[{"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/media?parent=12057707"}],"wp:term":[{"taxonomy":"faq-category","embeddable":true,"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/faq-category?post=12057707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}