Appending arrays in PHP -
i have 3 arrays represent category ids in wordpress, format $base_cat_id['term_id']
. want assign post 3 of these categories using following function:
wp_set_post_categories($entry['post_id'], $base_cat_id + $generic_n_cat_id + $specific_n_cat_id);
however, when post assigned first 2 categories. using right method add these arrays together?
edit:
i got work doing following:
$cat_ids = array($base_cat_id['term_id'], $generic_a_cat_id['term_id'], $specific_a_cat_id['term_id']); wp_set_post_categories($entry['post_id'], $cat_ids);
it's not pretty. found using array_merge same string id not work, overwrites values. union not work either can use union 2 arrays. please let me know if there better way!
use function array_merge(). arguments many arrays merging , returns arrays merged in 1 array, returns array.
like this:
wp_set_post_categories($entry['post_id'], array_merge($base_cat_id, $generic_n_cat_id, $specific_n_cat_id));
caveat: if arrays multilevel, might weird results. more info check out: http://php.net/manual/en/function.array-merge.php
Comments
Post a Comment