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"];

The second parameter to get_category_parents() defines whether a link to the category should be created. I'm guessing that it will work if you set it to false.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.