1

I've created a custom post type (roomtype). Now these roomtypes has post id 272 (single room in swedish) and 350 (double room in swedish) I've made it translatable (with WPML) and created in english. When I create new roomtypes with different languages (english) they get other id's: like 405 (single room) and 470 (double room)

I want to check some things in db and I make comparision against the roomtype-id for example single room.

Something like this:

//Check single room (both languages)
if ( $roomtype_id == 272 || $roomtype == 405 ) {
    //do action
}

//Check double room (both languages)
if ( $roomtype-id = 350 || $roomtype_id == 470 ) {
    //do action
}

How can I fetch the ID's associated with roomtype custom type indepently of language?

UPDATE: If someone runs into similar problem/issue:

I used apply_filters( 'wpml_object_id', $form_data['roomtype_id'], 'bookitall_roomtypes', false, 'sv' ); to get the id of roomtype in swedish for fetching and saving :-) In that way I did not have to the comparision between different languages at all.

I could have used taxonomy but actually that got confusing in the admin, it would be like having a roomtype and then a roomtype category where the roomtype category stated which roomtype it was. In many cases this would be great, but not in this case. WPML didn't show the translation of the taxonomy everywhere that I expected (Maybe it was about my coding, I didn't dig deeper). But thanks for the help!!!

Thank you for your help and giving alternatives!!!

  • How are you categorising rooms as single or double? Are you using a room type taxonomy? Or are you relying purely on the title? – Tom J Nowell Aug 1 '18 at 16:44
  • @TomJNowell - I'm relying on the ID and that worked when only using one language :-) (but for each language that is added one additional ID is used). I'm using ACF for adding some fields that are relevant for each roomtype. – bestprogrammerintheworld Aug 1 '18 at 18:49
  • To clarify. I'm not using any taxonomy to separate roomtypes, it's basically just a custom post type with custom fields. Value of these fields decide how many rooms of a certain roomtype that are available, how many nr of beds there are for a certain roomtype etc. The only reason I wanted the custom post type translated was that there are different names. The values should actually always be the same indenpently of the language for each roomtype. – bestprogrammerintheworld Aug 1 '18 at 19:24
  • I've actually came with up a new solution now, but it seems kind of "ugly". It is making my current post type "not translatable" (then no issues with id) and then just make a new custom type "translatable" that stores the name of the roomtypes only (which I can use for displaying in the booking form). There must be a better way of doing this... :-) – bestprogrammerintheworld Aug 1 '18 at 19:29
  • A custom taxonomy named "Room types" that had terms such as "single room" or "double room" would also work, and it would give you a pretty user interface. You could even translate the terms into other languages. It would even give you "Room types" archives with their own templates and URLs – Tom J Nowell Aug 1 '18 at 20:35
3

NOTE: I'm not giving a full answer to your question but trying to give some snippets that may help in WPML-based project.

I've used this function:

function get_the_translated_ID($id){
   if (!class_exists('sitepress')
    return $id;

  global $sitepress;
  $type=get_post_type($id);
  return icl_object_id( $id, $type, false, $sitepress->get_default_language());
}

This allows, for example, to search for custom_meta values of the original post instead of the translations.

Example in a loop:

$color = get_post_meta( get_the_translated_ID(get_the_ID() ), 'room_color',true);

Another useful one is to update some custom meta (the ones which has to be considered independent from language) in all the translations when updating a post in one lang:

function bulk_CF_update($post_id){
  if (!class_exists('sitepress')
    return;

  $thisPost=get_post($post_id);
  $allmeta=get_post_meta($post_id); // grab all custom meta fields of the post
  $toUnset = array("CF_1","CF_2","CF_3"); // exclude the CF you want to keep 'per-language'
  foreach($toUnset as $unset)
    unset($allmeta[$unset]);

  if($thisPost->post_type=="roomtype"){ // do it only for one or more specific cpt
    $trid = $sitepress->get_element_trid($thisPost->ID,'post_roomtype'); //note the prefix 'post_' has to be added to your cpt slug
    $translations = $sitepress->get_element_translations($trid);
    foreach($translations as $translation){
      foreach($allmeta as $meta=>$val){
        if(count($val)==1) //we're only managing single values
        update_post_meta($translation->element_id, $meta, $val[0]);
      }
    }
  }
}
add_action( 'save_post', 'bulk_CF_update',10,1);

Hope it may help

  • You got me going in right direction. I used apply_filters( 'wpml_object_id', $form_data['roomtype_id'], 'bookitall_roomtypes', false, 'sv' ); to get the id of roomtype in swedish for fetching and saving :-) In that way I did not have to the comparision between different languages at all. Thank you! – bestprogrammerintheworld Aug 2 '18 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.