1

So, I tried a bunch of ways to do this, but none worked.

I have an array, that has a lot of values from different taxonomies (from the same custom post type), I'm giving an example with only 2, but it can have 4, or 5, or 10.

Ex.: 
Types 1
    - Item a
    - Item b
    - Item c
Types 2
    - Item d
    - Item e
    - Item f

Those 'Item' values populate a <select>.

I need a way, to get the parent of the selected child. But the only info I will have is the slug and the name of the child. I will use that later for a tax_query.

I need to get if it's from Types 1, Types 2, etc.

What is the code I have now:

foreach ($tax_categorias as $tax_categoria) {
echo '<option value='. $tax_categoria->slug.'' . $selected . '>' . $tax_categoria->name . '</option>';

$ads_filt_categorias = $_GET['ads_filt_categorias'];
$parent_term = get_term_by('slug', $ads_filt_categorias);

I already tried other option, like use the slug to get the ID and then try to get the parent.

I tried get_terms(), get_ancestors(), etc

I'm probably missing something pretty basic... Any ideas what?

1

Since in most cases I would need to now the 'taxonomy', one work around I found (actually, pointe by @Howdy_McGee) was to pass the slug of the term and it's parent to the value, and then split-string.

Here is my final code:

foreach ($tax_categorias as $tax_categoria) {
    echo '<option value='. $tax_categoria->slug .'.'. $tax_categoria->taxonomy .'' . $selected . '>' . $tax_categoria->name . '</option>';
}
$ads_cat = $_GET['ads_filt_categorias'];
$list($ads_filt_categorias,$ads_categorias) = explode('.', $ads_cat);

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.