problem with get_post_custom()
-
I try to use sublanguage with toolset, but on certain post types i get the error:
Notice: Undefined offset: 0 in /wp-content/plugins/types/vendor/toolset/toolset-common/toolset-forms/classes/class.types.php on line 426Having this error also causes
Warning: Cannot modify header information – headers already sent by (output started at /wp-content/plugins/types/vendor/toolset/toolset-common/toolset-forms/classes/class.types.php:426) in /wp-admin/admin-header.php on line 9Which causes editing that post become impossible.
Here is the function from toolset with that error:
public static function getConditionalValues($post_id, $meta_type = 'postmeta') { $cond_values = array(); if ( !empty( $post_id ) ) { $cond_values = $meta_type == 'usermeta' ? get_user_meta( $post_id ) : get_post_custom( $post_id ); } // Unserialize [values] and do not allow array (take first value from array if ( is_array( $cond_values ) ) { foreach ( $cond_values as $k => &$v ) { $v = maybe_unserialize( $v[0] ); //THIS CAUSES THE ERROR $v = self::getStringFromArray($v); } } return $cond_values; }
the highlighted line is the problematic one.
After investigating the problems it seems that get_post_custom() while editing a post in the second language in my case returns the data in the wrong format.
From my observation get_post_custom returns an associative array with meta_keys as keys and array of values as the value.
With sublanguage the value may be an empty array (and that crashes toolset)
here is an example ofvar_dump(get_post_custom())
(i’ve cut out irrelevant lines, both fields are translatable, but only the text-lower does have a translation)array(25) { ["wpcf-main-text-upper"]=> array(0) { } ["wpcf-main-text-lower"]=> array(1) { [0]=> string(15) "translated text" } ["_en_wpcf-main-text-lower"]=> array(1) { [0]=> string(15) "translated text" } }
There is a workaround for toolset – just silencing the error with @, but it may cause problems in other places…
- The topic ‘problem with get_post_custom()’ is closed to new replies.