get_category_parents to array

Ok, So I get a choice from a wp-dropdown. This choice is a category.
I use this category to find it’s parents. This seem to be outputting a string.

This string should be converted into an array. And then I’d need to loop through the array and change the category name to a category ID.

But for some reason this doesn’t seem to work. What could I be doing wrong?
I commented out what doesn’t work.

(This array is intended to be used with post_category to create a new post.)

//Create array
$categoriesArray = [];

//Get choice from wp-dropdown
$selected_val = $_POST[‘cat’];

//Get parents from choice divided by (this seems to output a string)
$parents = get_category_parents( $selected_val, true, ‘,’ );

//Make array from string
$categoriesArray = explode(“,”,$parents);

for ($i = 0; $i < count($categoriesArray); $i++) {
$categoriesArray[$i] = get_cat_ID($categoriesArray[$i]);

//the array should look something like this.
//$categoriesArray = [“21″,”44”];

Read more here:: get_category_parents to array

Leave a Reply

Your email address will not be published. Required fields are marked *