Problem: I would like to create a View that displays CPTs from a different post type that have the same lowest-level taxonomy term as the current CPT or term archive page. It should be dynamic so it can be included in a widget on many different CPTs and archives.
Solution: Add the following custom shortcode, which will return the lowest-level taxonomy term for a specific CPT or Archive:
add_shortcode( 'ts_deepest_location', 'ts_deepest_location_func'); function ts_deepest_location_func($atts) { $id = $atts['id']; $terms = wp_get_post_terms( $id, 'locations', array( 'orderby' => 'id', 'order' => 'DESC' ) ); $deepestTerm = false; $maxDepth = -1; foreach ($terms as $term) { $ancestors = get_ancestors( $term->term_id, 'locations' ); $termDepth = count($ancestors); if ($termDepth > $maxDepth) { $deepestTerm = $term; $maxDepth = $termDepth; } } return $deepestTerm->slug; }
To display the View on a CPT, use this shortcode in a shortcode argument for a View filtered by taxonomy:
[wpv-view name="featured-breaks-for-markets-view" wpvlocations="[ts_deepest_location id="[wpv-post-id id='$current_page']"]"]
To display the View on a taxonomy Archive, use a wpv-taxonomy-archive shortcode in a shortcode argument for a View filtered by taxonomy:
[wpv-view name="featured-markets-for-locations-view" wpvlocations="[wpv-taxonomy-archive info='slug']"]
Be sure to register the ts_deepest_location function in Toolset > Settings > Front end Content > 3rd Party Shortcode arguments.
Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy
https://toolset.com/documentation/user-guides/passing-arguments-to-views/
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)
This topic contains 22 replies, has 3 voices.
Last updated by 7 years, 4 months ago.
Assisted by: Christian Cox.