Skip Navigation

[Resolved] Display for taxonomy archive list of other terms of the same level

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a taxonomy archive, and I would like to display a View of terms from the same hierarchical level as the current term.

Solution: Use custom shortcode to access the parent term ID, then use a View of terms filtered by parent term set by a shortcode attribute.

Relevant Documentation:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1210466

BV

Hi!

I created the custom taxonomy where each level = geographical area (Continents/Countries/States/Cities).

I made an archive template for custom post where I'm trying to call taxonomy view with filtering by parent.

But the features which I see only allows to filter view results by current archive taxonomy term AS a PARENT (option "Select taxonomy terms whose parent is the current taxonomy archive.)". When I need to filter view results by current archive taxonomy term PARENT AS a PARENT.

Means, if I show country archive - I want to show other countries in the block, not states. How to perform it?

#1210594

I made an archive template for custom post where I'm trying to call taxonomy view with filtering by parent.
In a taxonomy term archive, you can get the parent term ID with this custom shortcode:

function get_term_parent_func($atts) {
  $a = shortcode_atts( array(
      'taxonomy' => '',
      'return' => 'id'
  ), $atts );
  $taxonomy = $a['taxonomy'];
  $parent_id = 0;
  if( is_tax( $taxonomy ) ) {
    $taxObj = get_queried_object();
    $tax = isset($taxObj->taxonomy) ? $taxObj->taxonomy : 0;
    $term = isset($taxObj->term_id) ? $taxObj->term_id : 0;
    $parent_slug = get_term_parents_list( $term, $tax, array('format'=>'slug', 'link'=>FALSE, 'inclusive'=>FALSE, 'separator'=>''));
    if( $a['return'] == 'id' ) {
      $parent = get_term_by( 'slug', $parent_slug, $tax );
      $parent_id = isset($parent->term_id) ? $parent->term_id : $taxObj->term_id;
      return $parent_id;
    }

    if( $a['return'] == 'slug' ) {
      return $parent_slug;
    }
  }
  return $parent_id;
}
add_shortcode("get_term_parent", "get_term_parent_func");

So in a taxonomy archive for the "category" taxonomy, you would use the shortcode like this:

[get_term_parent_id taxonomy='category']

You must register this shortcode to be used as a third-party shortcode attribute. Go to Toolset > Settings > Front-end Content and add get_term_parent_id in third party shortcode arguments.

Then, you can create a View of the taxonomy filtered by parent term ID, set by a shortcode attribute "terms". Use the custom shortcode to set the parent term ID, like this:

[wpv-view name="Your sibling term View" terms="[get_term_parent_id taxonomy='category']"]

Then in the loop of that View, output whatever information you want to display for all the sibling terms.

#1212281

BV
Screenshot 2019-03-10 at 23.06.43.png

I'm a bit modified your code because I have up to 4 level terms when you was written for the case of 2 lvls, huuuh, dived to the PHP + WP coding, long time didn't do it… Now the function returns real parent term id.

But now I have problems with passing parent term id. Yes, I found that if you filter by "taxonomy term" - it's clearly visible you can pass terms ids in the "terms" or any custom field.

But for "taxonomy parent" case I have no idea how to pass it. It's only said "Parent is the taxonomy selected by the parent Taxonomy View" and nothing more! Pls, clarify what it meaning and how to set the parent term id, or maybe it's need to update plugins to make same set of parameters for parent filter?

P. S. I added 2 filters for make the screenshot to visualise the difference to you. Of course in experiments I'm using the only "taxonomy parent" filter.

P. P. S. I'm pretty sure that passing parent term id by terms="" parameter not works because I tried to set even numbers (real ids of terms) in [wpv-view name="Your sibling term View" terms=""], with any values view returns ALL taxonomy terms of all levels.

#1212286

BV

Ohh, I realised I have to use nested views 🙂

#1212287

BV

My issue is resolved now. Thank you!

#1212291

BV

Ohh, everything works fine except cases, when for the main archive loop nothing found. In this case for the bottom view, which displaying neighbour places, nothing outputs either (but have to):

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<ul class="wpv-loop js-wpv-loop">
		<wpv-loop>
			<li>[wpv-post-body view_template="loop-item-in-places-locations"]</li>
		</wpv-loop>
	</ul>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

<h2>neighbour places</h2>
[wpv-view name="places-location-in-neighbourhood-by-term-id" terms="[get_term_parent_id taxonomy='places-location']"]
#1212745

Okay can you make this change and tell me what you see for "Term parent ID"?

<h2>neighbour places</h2>
Term parent ID: [get_term_parent_id taxonomy='places-location']<br />
[wpv-view name="places-location-in-neighbourhood-by-term-id" terms="[get_term_parent_id taxonomy='places-location']"]

Then edit the places-location-in-neighbourhood-by-term-id View and take a screenshot of the Query Filters so I can see how the View is configured.

#1212894

BV

Function works properly - I see the correct id (parent term id). But view call - it's looks like it not works at all. Maybe some exception in the loop stops calling of any views? I had this issue even before I asked of your help and tried to call some other views, but didn't notice it.

Now I have also view which calls top level terms (w/o any parameters), I call it:

[wpv-view name="places-location-top-level"]

and output for empty loops is the same!

#1213264

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Christian is sick today. As he is already dealing with and familiar with this issue I'll let him resume with it when he returns tomorrow. (If he is unable to I will take it on.)

#1213339

BV

Yeah, ok!

#1213716

Hi, can you copy + paste the code from the "places-location-in-neighbourhood-by-term-id" View's Loop Editor here for me to review? Can you take screenshots showing the entire View editor screen in wp-admin?

#1214060

BV

Maybe I better give you access to the website? Because we talk about nested views and also I call other view I mentioned before from that block (depends on level of taxonomy term in current archive), so I suppose it would be faster.

#1214257

Okay that makes sense, please provide access in the private reply fields here.

#1215154

It looks like the Content Templates used in the View here had become corrupted:
hidden link
hidden link

I rebuilt those loops without using Content Templates, and now I can see the neighbour places appear. Please take a look and let me know if it is not resolved.

#1232677

BV

I'm ok now, thanks!