Create a metadata with empty value to parent compound metadata

This commit is contained in:
vnmedeiros 2020-05-06 18:58:10 -03:00
parent dd6f04a6d8
commit c2147722d8
2 changed files with 13 additions and 1 deletions

View File

@ -56,7 +56,17 @@ class Item_Metadata extends Repository {
if ( $metadata_type->get_primitive_type() == 'term' ) {
$this->save_terms_metadatum_value( $item_metadata );
} elseif ( $metadata_type->get_primitive_type() == 'compound' ) {
// do nothing. Compound values are updated when its child metadata are updated
// Create a post_metadata with value [] to compound metadata or return if it already exists
// to use as parent_meta_id for child metadata
global $wpdb;
$compounds = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s and meta_value = ''", $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id() ), ARRAY_A );
if( is_array($compounds) && !empty($compounds) ) {
$meta_id = $compounds[0]['meta_id'];
$item_metadata->set_parent_meta_id($meta_id);
} else {
$meta_id = add_post_meta( $item_metadata->get_item()->get_id(), $item_metadata->get_metadatum()->get_id(), '' );
$item_metadata->set_parent_meta_id($meta_id);
}
return $item_metadata;
} else {
if ( $unique ) {

View File

@ -169,6 +169,8 @@ class Compound extends Metadata_Type {
public function get_value_as_html(Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value();
$return = '';
if ( empty($value) )
return $return;
$separator = $item_metadata->get_multivalue_separator();
if ( $item_metadata->is_multiple() ) {