Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 14:00 9:00 – 14:00 9:00 – 14:00 9:00 – 14:00 9:00 – 14:00 -
- 19:00 – 22:00 19:00 – 22:00 19:00 – 22:00 19:00 – 22:00 19:00 – 22:00 -

Supporter timezone: Europe/Paris (GMT+02:00)

This topic contains 16 replies, has 2 voices.

Last updated by Yvette 1 year, 9 months ago.

Assisted by: Yvette.

Author Posts
June 27, 2022 at 10:46 am #11549873

fredrikV-2

get_post_type_object() returns the original language

June 27, 2022 at 2:30 pm #11552365

Yvette
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/Paris (GMT+02:00)

Hello

I will be helping you.

Unfortunately, I cannot see the chat history for this thread. Please tell me if you attempted to use the following API hook/filter to retrieve the correct post type ID :
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/#hook-605256

Would you be able to create a very simple function that uses a snippet of your code in a new sandbox site to see if the logic you are using is valid for retrieving a translated object information?

Thanks

June 30, 2022 at 6:16 am #11572289

fredrikV-2

Hi,
yes we tried that during the chat. The $post is the English version of the post but when using $post_type = get_post_type($post) and then get_post_type_object($post_type), the response is an object with the post type information in Swedish. We tried it with the woocommerce product post type.

June 30, 2022 at 6:17 am #11572291

fredrikV-2

Btw, this is done outside of templates. It's in WP-CLI code that populates Algolia search indexes. We want the index to have a field with the post type name in the language of the index.

June 30, 2022 at 8:25 am #11573315

Yvette
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/Paris (GMT+02:00)

Hello

You wrote: "Btw, this is done outside of templates."

I see. Then this may be out of scope for our forum support unfortunately. In general, our filters and hooks work within the Worpdress environment.

Please see the response here:
https://wpml.org/forums/topic/wp-cli-commands-for-wpml/#post-11353187

I will double-check that this missing feature is also true for WPML but I wanted to get back to you to set expectations appropriately.

June 30, 2022 at 8:35 am #11573405

Yvette
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/Paris (GMT+02:00)

Hello

I talked with 2nd tier support...they have said it SHOULD work if you have registered commands (which it seems you have done).

In this case, they ask if you will please send us the snippet that you are using to see if they can comment on it.

They may also ask you to try this out on a sandbox site which I will provide to you but first, let's just see your code.

Thanks

June 30, 2022 at 8:42 am #11573435

fredrikV-2

Here are all parts involved in trying to get the post type name in English. The reindex_posts is called with --lang=en so everything is in English except the name of the post type.

  /**
   * Reindex by post type and language in wp-cli
   * Example use: wp algolia reindex_posts --type=post --lang=en
   */
  public function reindex_posts($args, $assoc_args) {
    $lang = isset($assoc_args['lang']) ? $assoc_args['lang'] : 'sv';
    $algolia = 'algolia_' . $lang;
    global $$algolia;
    global $wpdb;

    $current_lang = apply_filters( 'wpml_current_language', NULL );
    do_action('wpml_switch_language', $lang);

    $type = isset($assoc_args['type']) ? $assoc_args['type'] : 'post';

    $indexName = $wpdb->prefix . $type;
    $index = $$algolia->initIndex(
      apply_filters('algolia_index_name', $indexName, $type)
    );
    $index->setSettings(['indexLanguages' => ["{$lang}"]]);
    $index->clearObjects()->wait();

    $paged = 1;
    $count = 0;

    do {
      $posts = new WP_Query([
        'posts_per_page' => 100,
        'paged' => $paged,
        'post_type' => $type,
        'post_status' => 'publish',
      ]);

      if (!$posts->have_posts()) {
        break;
      }

      $records = [];

      foreach ($posts->posts as $post) {
        if (!empty($assoc_args['verbose'])) {
          WP_CLI::line('Indexing [' . $post->post_title . ']');
        }
        $record = (array) apply_filters($type.'_to_record', $post);

        if (! isset($record['objectID'])) {
          $record['objectID'] = implode('#', [$post->post_type, $post->ID]);
        }

        $records[] = $record;
        $count++;
      }

      $index->saveObjects($records);

      $paged++;

    } while (true);

    do_action('wpml_switch_language', $current_lang);

    WP_CLI::success("$count $type entries indexed in " . $algolia);
  }
/**
 * Create index record for products
 */
function mk_algolia_product_to_record($post) {
  return array(
    'objectID' => implode('#', [$post->post_type, $post->ID]),
    'post_type' => get_post_type($post),
    'post_type_label' => mk_algolia_get_post_type_name($post),
    'title' => $post->post_title,
    'excerpt' => strip_tags($post->post_excerpt),
    'content' => mk_algolia_parse_blocks($post->post_content),
    'url' => get_permalink($post->ID),
    'thumbnail' => get_the_post_thumbnail_url( $post->ID, 'thumbnail' ),
    'brand' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_brand')),
    'category' => mk_algolia_get_terms(wp_get_post_terms($post->ID, 'product_cat')),
    'usagearea' => mk_algolia_get_terms(wp_get_post_terms($post->ID, 'pa_usagearea')),
    'color' => mk_algolia_get_terms(wp_get_post_terms($post->ID, 'pa_color')),
    'bladelength' => mk_algolia_get_single_term_numeric(wp_get_post_terms($post->ID, 'pa_bladelength')),
    'bladethickness' => mk_algolia_get_single_term_numeric(wp_get_post_terms($post->ID, 'pa_bladethickness')),
    'bladeshape' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_bladeshape')),
    'blunttip' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_blunttip')),
    'childfriendly' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_childfriendly')),
    'edgeprotection' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_edgeprotection')),
    'firesteelcompatible' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_firesteelcompatible')),
    'flexgrade' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_flexgrade')),
    'floating' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_floating')),
    'gripmaterial' => mk_algolia_get_terms(wp_get_post_terms($post->ID, 'pa_gripmaterial')),
    'handedness' => mk_algolia_get_terms(wp_get_post_terms($post->ID, 'pa_handedness')),
    'knifelength' => mk_algolia_get_single_term_numeric(wp_get_post_terms($post->ID, 'pa_knifelength')),
    'knifeseries' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_knifeseries')),
    'knifetype' => mk_algolia_get_terms(wp_get_post_terms($post->ID, 'pa_knifetype')),
    'material' => mk_algolia_get_terms(wp_get_post_terms($post->ID, 'pa_material')),
    'mollecompatible' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_mollecompatible')),
    'netweight' => mk_algolia_get_single_term_numeric(wp_get_post_terms($post->ID, 'pa_netweight')),
    'otherinfo' => mk_algolia_get_terms(wp_get_post_terms($post->ID, 'pa_otherinfo')),
    'serratededge' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_serratededge')),
    'serratedspine' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_serratedspine')),
    'sheathorientation' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_sheath-orientation')),
    'size' => mk_algolia_get_terms(wp_get_post_terms($post->ID, 'pa_size')),
    'steeltype' => mk_algolia_get_single_term(wp_get_post_terms($post->ID, 'pa_steeltype')),
    'totallength' => mk_algolia_get_single_term_numeric(wp_get_post_terms($post->ID, 'pa_totallength')),
  );
}
add_filter( 'product_to_record', 'mk_algolia_product_to_record', 10, 1 );
/**
 * Get post type name
 */
function mk_algolia_get_post_type_name($post) {
  $post_type = get_post_type( $post );
  $post_type_object = get_post_type_object( $post_type );

  return $post_type_object->label;
}
June 30, 2022 at 8:47 am #11573445

fredrikV-2

The filters we tried in the chat I have removed again since they made no difference.

July 4, 2022 at 6:56 am #11594743

Yvette
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/Paris (GMT+02:00)

Hello

The response from 2nd tier: "try putting

 suppress_filters => false 

and see if this helps.

Please remember that our support for debugging custom code is extremely limited. Our support forum is provided to help clients use our plugins out-of-the-box with standard commercial plugins and themes that have been tested compatible with WPML.

In other cases, we can advise and guide but not troubleshoot. For this, at some point, we will have to direct you to a WPML contractor who can take your particular case on.

July 7, 2022 at 9:16 am #11620681

fredrikV-2

Hello,
sorry for the delay. I've tried it now but no difference.

July 8, 2022 at 8:18 am #11628687

Yvette
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/Paris (GMT+02:00)

I have forwarded your response to 2nd tier - am waiting for some more suggestions/guidance.

July 8, 2022 at 10:22 am #11629945

Yvette
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/Paris (GMT+02:00)

I have been instructed to ask for a clone of your site as per this article.
https://wpml.org/faq/provide-supporters-copy-site/

The 2nd tier team will be using this cloned site to debug your code.

Please use WeTransfer or similar to provide the package and include the downloadable link in your next reply. I am opening the private area for this link.

July 8, 2022 at 10:34 am #11630017

Yvette
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/Paris (GMT+02:00)

I forgot to open the private are.

July 11, 2022 at 11:45 am #11642089

fredrikV-2

I think something went wrong. I could still see the content when not logged in. Can you open the private area again?

July 11, 2022 at 12:51 pm #11642695

Yvette
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/Paris (GMT+02:00)

I am opening the private area again.

The topic ‘[Closed] get_post_type_object() returns the original language’ is closed to new replies.