Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Terms query of taxonomy with 4 leves WordPress

  • SOLVED

Hi I need get terms for level 1-3 (Not level 4)

I dont wont to make 3 different queries... just one.

the level 1-3 is a custom taxonomy called "location"

Level 1 = Region
Level 2 = Province
Level 3 = Coasts

So i am trying to print it out like this:


<h2>Regions (Level 1)</h2>
<ul>
<li>Region 1</li>
<li>Region 2</li>
<li>Region 3</li>
<li>Etc. Etc.</li>
</ul>

<h2>Provinces (Level 2)</h2>
<ul>
<li>Province 1</li>
<li>Province 2</li>
<li>Province 3</li>
<li>Etc. Etc.</li>
</ul>

<h2>Coasts (Level 3)</h2>
<ul>
<li>Coasts 1</li>
<li>Coasts 2</li>
<li>Coasts 3</li>
<li>Etc. Etc.</li>
</ul>


I have only been able to print out the level 1 and level 2 in two separate queries. I woudl like it to be one query.

Thanks in advance

Answers (2)

2015-10-03

dimadin answers:

You have a problem in naming things. It not clear what is term, its child term, what is post attached to that term, how your response should look like using those names.

I am assuming you have organization like:

Term 1
- Subterm 1
- Subterm 2
Term 2
- Subterm 3
- Subterm 4
...

To get display of those terms this way, this code works (to replace 'category' with actual taxonomy):


$categories = get_terms( 'category', array(
'hide_empty' => 0,
) );

$subcategories = $categories;

foreach ( $categories as $category ) {
// Only top level terms
if ( 0 != $category->parent ) {
continue;
}

// It is first level, display it
echo '<h2>' . $category->name . '</h2>';
echo '<ul>';

foreach ( $subcategories as $subcategory ) {
// Only child terms
if ( $category->term_id != $subcategory->parent ) {
continue;
}

echo '<li>' . $subcategory->name . '</li>';
}

echo '</ul>';
}


If your organization is not like this, please describe how it actually is, or even better, post screenshots of your WordPress admin with those things you want to better understand what is a goal


reneberg comments:

Hi dimadin,

I started the message with this:

Hi I need get terms for level 1-3 (Not level 4)

I dont wont to make 3 different queries... just one.

the level 1-3 is a custom taxonomy called "location"

Maybe this can be misunderstood? if so i need to be more clear.

Your answer looks perfect all though the query only get´s 2 levels. As mentioned i may question, i have 3 levels in this taxonomy.

I will try to explain in another way:

(TITLE FOR LIST: REGIONS)
Term 1 (LEVEL 1)
- Subterm 1.1
- Subterm 1.2
(TITLE FOR LIST: PROVINCES)
Term 2 (LEVEL 2)
- Subterm 2.1
- Subterm 2.2
(TITLE FOR LIST: COASTS)
Term 3 (LEVEL 3)
- Subterm 3.1
- Subterm 3.2

Again if somebody misunderstood my question, i apologize for not being more clear. I am still learning :-)


reneberg comments:

Hi dimadin,

Your code is perfect, only thing missing is to get the 3. level as well


reneberg comments:

Hi dimadin,

Your code is perfect, only thing missing is to get the 3. level as well


reneberg comments:

Try to explain in another way...

i want 3 lists:

1 list with LEVEL 1 terms
1 list with LEVEL 2 terms
1 list with LEVEL 3 terms


dimadin comments:

So, for exampe, 'Regions' is first level term, 'Term 1' is its child, or second level term, and 'Subterm 1.1' is child of 'Term 1', or third level term?

Because how you name levels is wrong, Level 1, Level 2 etc actualyy look like siblings, not like parent and child. To have different level you should have

Level 1
- Level 2
- Level 3

not

Level 1
Level 2
Level 3

This second case are not separate levels, they are on the same level.


dimadin comments:

I am not sure what you name Level 3, but this is for how Level 3 should look like:


$categories = get_terms( 'category', array(
'hide_empty' => 0,
) );

$subcategories = $subsubcategories = $categories;

foreach ( $categories as $category ) {
// Only top level terms
if ( 0 != $category->parent ) {
continue;
}

// It is first level, display it
echo '<h2>' . $category->name . '</h2>';
echo '<ul>';

foreach ( $subcategories as $subcategory ) {
// Only child terms
if ( $category->term_id != $subcategory->parent ) {
continue;
}

// It is second level, display it
echo '<li>' . $subcategory->name;
echo '<ul>';

foreach ( $subsubcategories as $subsubcategory ) {
// Only child terms
if ( $subcategory->term_id != $subsubcategory->parent ) {
continue;
}

// It is third level, display it
echo '<li>' . $subsubcategory->name . '</li>';
}

echo '</ul>';
echo '</li>';
}

echo '</ul>';
}


dimadin comments:

There is a formatting error in my message from 11:08, to better describe leveling:


Level 1
- Level 2
-- Level 3


not


Level 1
Level 2
Level 3


reneberg comments:

Hi,

i dont now how to explain this. The latest code gets in example Level 1 with its children, then Level 2 with its children.

I do not need to list childrens.

i need 3 lists

one of each level.

i need 3 lists:

1 list with LEVEL 1 terms
1 list with LEVEL 2 terms
1 list with LEVEL 3 terms

In 1. list all terms that are on level 1 (no children)
In 2. list all terms that are on level 2 (no children)
In 3. list all terms that are on level 3 (no children)

I have attached an image so you can see the taxonomy that consists of 4 levels...


dimadin comments:

It looks like your image wasn't attached.

Sorry, but I really don't follow how does your use case looks, maybe with some screenshot I would understand better. I probably can create workable solution but first I need to know exact situation.


reneberg comments:

Hi here is image from taxonomy

I only need to have 3 lists...

1 list with all terms for level 1
1 list with all terms for level 2
1 list with all terms for level 2