Problem: I have a View of a CPT. There will only be 1 result in this View #1. I would like to filter View #2 by taxonomy, where the terms are the same as the terms applied to the results of View 1.
Solution:
Create a duplicate of View 1. In the Loop Output editor, insert the wpv-post-taxonomy shortcode to output the term slugs associated with the post. Use a comma separator. Place this View #3 just before View #2 on the same page. The results should look like this: slug1, slug2, slug3
- Edit View #2. Apply a taxonomy Query Filter, where the term slugs are set by a shortcode attribute. See view-2-filter.png for a similar example. My taxonomy is "Book Taxes" so yours will be a bit different, but the idea is the same.
- Now add the shortcode attribute to the shortcode you used to insert View #2, something like this:
[wpv-view name="view-2-slug" wpvbooktax="term-slug"]
- Test by using a real term slug in the shortcode attribute, and be sure the View is showing the matching results
- Replace the real term slug with the results of View #3 by inserting View #3 shortcode inside the taxonomy shortcode attribute for View #2.
- Add this code to your functions.php file:
add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 ); function prefix_clean_view_output( $out, $id ) { $ids = array( 12345 ); if ( in_array( $id, $ids )) { $start = strpos( $out, '<!-- wpv-loop-start -->' ); if ( $start !== false && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false ) { $start = $start + strlen( '<!-- wpv-loop-start -->' ); $out = substr( $out , $start ); $end = strrpos( $out, '<!-- wpv-loop-end -->' ); $out = substr( $out, 0, $end ); } else { $start = strpos( $out, '>' ); if ( $start !== false) { $out = substr( $out, $start + 1 ); $end = strpos( $out, '<' ); $out = trim(substr( $out, 0, $end )); } } } return $out; }
Change "12345" to be the numeric ID of View #3. This code strips the unnecessary markup from the results of View #3 so the slugs can be read by the Query Filter of View #2.
Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy
https://toolset.com/documentation/user-guides/passing-arguments-to-views/
https://toolset.com/documentation/user-guides/filtering-views-by-taxonomy/
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.
Our next available supporter will start replying to tickets in about 8.81 hours from now. Thank you for your understanding.
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 6 replies, has 2 voices.
Last updated by 6 years, 6 months ago.
Assisted by: Christian Cox.