Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
You are using WP-CLI to list all media IDs in the trash across all languages but the command only returns media IDs for the main language.
Solution:
We have identified two potential workarounds to include media IDs from all languages:
1) Use the WP-CLI parameter

--suppress_filters=true

which allows the command to work across all languages.
2) Use the parameter

--url="https://yourwebsite.com/en"

to force WP-CLI to work in the language based on the URL.

Additionally, our 2nd Tier Support suggests modifying the code in

wp-content/plugins/sitepress-multilingual-cms/classes/query-filtering/wpml-query-filter.class.php

. Replace the existing

posts_where_filter

function with the following code to include a condition that checks for the 'lang' parameter in the query:

public function posts_where_filter( $where, $query ) {
	if ( $query === null || $this->where_filter_active( $query ) === false ) {
		return $where;
	}

	$requested_id = isset( $_REQUEST['attachment_id'] ) && $_REQUEST['attachment_id'] ? $_REQUEST['attachment_id'] : false;
	$requested_id = isset( $_REQUEST['post_id'] ) && $_REQUEST['post_id'] ? $_REQUEST['post_id'] : $requested_id;
	$requested_id = (int) $requested_id;

	$default_language = $this->sitepress->get_default_language();

	$post_language = $this->post_translations->get_element_lang_code( $requested_id );
	if (isset($query->query["lang"])) {
		$post_language = $query->query["lang"];
		$requested_id = "1"; //hack needed because then $current_language will not get the $post_language, but will call for get_current_language
	}

	$current_language = $requested_id && $post_language ? $post_language : $this->sitepress->get_current_language();

	$condition = $current_language === 'all' ? $this->all_langs_where() : $this->specific_lang_where( $current_language, $default_language );
	$where     .= $condition;

	return $where;
}

Please note that this solution might be outdated or not applicable to your specific case. We highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If these solutions do not resolve your issue, please open a new support ticket at WPML support forum.

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.

This topic contains 16 replies, has 2 voices.

Last updated by Kor 1 week, 2 days ago.

Assisted by: Kor.

Author Posts
October 31, 2024 at 4:48 pm #16352820

Dimitri

Background of the issue:
I am trying to use WP-CLI to list all media IDs in the trash across all languages. I used the command: wp post list --post_type='attachment' --post_status=trash --format=ids.

Symptoms:
The command only returns a list of media IDs in the trash for the main language, not for all languages.

Questions:
How can I modify the WP-CLI command to include media IDs from all languages?
Is there a setting in WPML that affects how WP-CLI retrieves media IDs?

October 31, 2024 at 5:10 pm #16353003

Kor
Supporter

Languages: English (English )

Timezone: Asia/Singapore (GMT+08:00)

Thanks for your patience.

Here is the ticket. I will need to closely examine your website. Therefore, I will require temporary access (WP-Admin and FTP) to a test site where the problem has been replicated, if possible. This will enable me to provide better assistance and determine if any configurations need to be modified.

Please note that it is important to have a backup for security purposes and to prevent any potential data loss. You can use the UpdraftPlus plugin (https://wordpress.org/plugins/updraftplus/) to fulfill your backup requirements.

Your upcoming reply will be treated with confidentiality and will only be visible to you and me.

✙ To resolve the issue, I may need your consent to deactivate and reactivate plugins and the theme, as well as make configuration changes on the site if necessary. This emphasizes the significance of having a backup.

October 31, 2024 at 5:15 pm #16353007

Dimitri

Could you please check on a basic sandbox website ? My website contains copyright code and tons of images.
It is easier to check this on an empty sandbox site.

October 31, 2024 at 5:28 pm #16353018

Kor
Supporter

Languages: English (English )

Timezone: Asia/Singapore (GMT+08:00)

Thanks for your reply.

Sorry, we do not have a sandbox site that we can use to run WP-CLI commands. Could you prepare a sandbox site from your end and also, share access where we can run the WP-CLI commands together?

October 31, 2024 at 6:16 pm #16353082

Kor
Supporter

Languages: English (English )

Timezone: Asia/Singapore (GMT+08:00)

Thanks for your reply.

I've checked and I cannot locate any media in English language. hidden link as shown in the attached screenshot.

Perhaps you can run this media feature to duplicate the media? hidden link

WPML > Settings > Media File Translation

Screenshot_153.png
Screenshot_154.png
October 31, 2024 at 6:21 pm #16353116

Dimitri

Yes, i've put a media in TRASH in english language.
See attachment

Capture d’écran 2024-10-31 192037.png
October 31, 2024 at 6:24 pm #16353140

Kor
Supporter

Languages: English (English )

Timezone: Asia/Singapore (GMT+08:00)

Thanks for your reply.

So you would like to be able to list the media from English that is in the trash bin. Is this what you're trying to achieve?

October 31, 2024 at 6:28 pm #16353158

Dimitri

See that the wp-cli command return nothing when I have one item in English

Capture d’écran 2024-10-31 192658.png
October 31, 2024 at 6:32 pm #16353183

Dimitri

Yes, I'd like to be able to list all medias for ALL languages in trash.

The goal is to delete them after some php functions.

WPML make posts unavailable in secondary languages. It is like there is a filter somewhere.

October 31, 2024 at 6:32 pm #16353188

Kor
Supporter

Languages: English (English )

Timezone: Asia/Singapore (GMT+08:00)

Try this.

wp post list --post_type=attachment --format=ids --lang=en --post_status=trash
October 31, 2024 at 6:34 pm #16353189

Dimitri

Do not work, sorry

October 31, 2024 at 6:39 pm #16353196

Dimitri

Anyway, if I try to list all pages Ids, I have the same issue, it lists only pages in main language.
In example below, it display 19, 3 (french ids)

Capture d’écran 2024-10-31 193859.png
October 31, 2024 at 6:40 pm #16353211

Kor
Supporter

Languages: English (English )

Timezone: Asia/Singapore (GMT+08:00)

I'm checking this locally.

October 31, 2024 at 6:49 pm #16353223

Kor
Supporter

Languages: English (English )

Timezone: Asia/Singapore (GMT+08:00)

I can see the issue now. --lang=en filter doesn't seem to work or exist. I need to check with our 2nd Tier Support about this and I will come back to you as quickly as possible.

November 4, 2024 at 1:22 pm #16361190

Kor
Supporter

Languages: English (English )

Timezone: Asia/Singapore (GMT+08:00)

Thanks for your patience.

Our 2nd Tier Support checked and they found a workaround by replacing wp-content/plugins/sitepress-multilingual-cms/classes/query-filtering/wpml-query-filter.class.php

public function posts_where_filter( $where, $query ) {
		if ( $query === null || $this->where_filter_active( $query ) === false ) {
			return $where;
		}

		$requested_id = isset( $_REQUEST['attachment_id'] ) && $_REQUEST['attachment_id'] ? $_REQUEST['attachment_id'] : false;
		$requested_id = isset( $_REQUEST['post_id'] ) && $_REQUEST['post_id'] ? $_REQUEST['post_id'] : $requested_id;
		$requested_id = (int) $requested_id;

		$default_language = $this->sitepress->get_default_language();

		$post_language = $this->post_translations->get_element_lang_code( $requested_id );
		$current_language = $requested_id && $post_language ? $post_language : $this->sitepress->get_current_language();

		$condition = $current_language === 'all' ? $this->all_langs_where() : $this->specific_lang_where( $current_language, $default_language );
		$where     .= $condition;

		return $where;
	}

With

	public function posts_where_filter( $where, $query ) {
		if ( $query === null || $this->where_filter_active( $query ) === false ) {
			return $where;
		}

		$requested_id = isset( $_REQUEST['attachment_id'] ) && $_REQUEST['attachment_id'] ? $_REQUEST['attachment_id'] : false;
		$requested_id = isset( $_REQUEST['post_id'] ) && $_REQUEST['post_id'] ? $_REQUEST['post_id'] : $requested_id;
		$requested_id = (int) $requested_id;

		$default_language = $this->sitepress->get_default_language();

		$post_language = $this->post_translations->get_element_lang_code( $requested_id );
//start hack
		if (isset($query->query["lang"])) {
			$post_language = $query->query["lang"];
			$requested_id = "1"; //hack needed because then $current_language will not get the $post_language, but will call for get_current_language
		}
//end hack
		$current_language = $requested_id && $post_language ? $post_language : $this->sitepress->get_current_language();

		$condition = $current_language === 'all' ? $this->all_langs_where() : $this->specific_lang_where( $current_language, $default_language );
		$where     .= $condition;

		return $where;
	}

This is just a workaround and it is still under our management review if this can be implemented more intelligently and have a better outcome.