returns with an error (array_map(): Argument #2 should be an array),
as no array is present
The $states['s_bib_source_c_bib_sources'] ?? []
is only setting $states['s_bib_source_c_bib_sources']
if it isn’t already set. If it’s set and it’s not an array, then array_map
would throw the “should be an array” error. So you might better off use type-casting:
isset( $states['s_bib_source_c_bib_sources'] ) ? // wrapped
(array) $states['s_bib_source_c_bib_sources'] : []
EDIT: Correct me if the $bib_sources['c_bib_sources_abbreviated_reference']
is not a WP_Term
instance..
The get_term()
part here is also confusing, because term IDs can never be the same and whatever the taxonomy is, the taxonomy’s terms are saved in the same database table:
get_term_by( 'id', $bib_sources['c_bib_sources_abbreviated_reference']->term_id, 'bib_sources') ?? null
and the whole code could have probably been rewritten to:
$bib_sources['c_bib_sources_abbreviated_reference'] ?? null
However, if you want to re-verify that the array item c_bib_sources_abbreviated_reference
is a valid term, then you could do so:
array_map( function( $bib_sources ) {
$abb_ref = isset( $bib_sources['c_bib_sources_abbreviated_reference'] ) ?
get_term( $bib_sources['c_bib_sources_abbreviated_reference'], 'bib_sources' ) : null;
return [
'type' => $bib_sources['c_bib_sources_type'] ?? null,
'year' => $bib_sources['c_bib_sources_year'] ?? null,
'abb_ref' => is_wp_error( $abb_ref ) ? null : $abb_ref, // <- this
'citation' => $bib_sources['c_bib_sources_citation'] ?? null,
];
}, isset( $states['s_bib_source_c_bib_sources'] ) ? (array) $states['s_bib_source_c_bib_sources'] : [] );