[Resolved] Crash when a custom post type has no posts
This thread is resolved. Here is a description of the problem and solution.
Problem: The client was experiencing a crash when trying to create a normal post in a custom post type, specifically when there were no posts in the custom post type.
Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in /wp-content/plugins/sitepress-multilingual-cms/menu/post-menus/wpml-posts-listing-page.class.php:26
Solution: We recommend adding a conditional check to the relevant file to prevent the crash. Insert the following code to the file
This code ensures that if there are no posts, the function will return early and prevent the crash.
Please note that this solution might be outdated or not applicable to your case. If you're still experiencing issues, we highly recommend checking the related known issues, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If the problem persists, please open a new support ticket with us.
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.
I am trying to: create a normal post in a custom post type
Link to a page where the issue can be seen: see screenshot
I expected to see: no crash
Instead, I got: a crash
Wordpress 6.4.2
Wordpress multisite subfolder
WPML Multilingual CMS Version 4.6.9
WPML String Translation Version 3.2.9
The expected fix is to edit the file (in the screenshot), add this in line 26
```
if ( empty($wp_query->posts) )
return;
```
Or make sure that if it is NULL then do return.
Languages: English (English )Spanish (Español )Portuguese (Brazil) (Português )
Timezone: America/Sao_Paulo (GMT-03:00)
Hello, welcome to the WPML Support forum!
While you are waiting for one of my colleagues to take this ticket and work on it, let me provide you with first debugging steps or if I can perhaps help with the issue quickly.
Please try the following workaround:
- Open the wp-content/plugins/sitepress-multilingual-cms/classes/wp-core-hooks/post/class-wpml-remove-pages-not-in-current-language.php file
- Around line 34, replace the following snippet:
Hello, I cannot give you a backup since I already moved on from this issue. I already applied the workaround and I'm just sharing to you that there is this problem where a crash can occur.
The crash occurs when there is no posts inside a custom post type, not sure why it does occur but more info is in the screenshot.
I'm assuming it is clear in the screenshot and based on the workarounds suggested.
If you assume that the info I'm sharing is not substantive enough, then you can just close this support issue.