Skip Navigation

[Resolved] Filter a View by terms applied the results of another View

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

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 support ticket is created 6 years, 7 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.

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 tobiasB-4 6 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#803168

Hi

I’ve searched the forum but haven’t figured this out. Hope you can help me.

I have a page with a view (view 1) that shows a post from a custom post type (CPT A). The view includes, among custom fields, a taxonomy.

On the same page, I have another view (view 2), which comes from another custom post type (CPT B) and includes the same taxonomy as in view 1.
Now I want view 2 to match view 1 by that taxonomy.

I’ve tried to set the taxonomy filter in view 2 to filter by “Value set by the current page where this View is shown” but this seems to work only if you insert the view to the post template for example.

Any idea how to make this work?

/ Tobias

#803994

Now I want view 2 to match view 1 by that taxonomy.
If I understand you correctly, it sounds like you want to filter View 2 by the taxonomy terms associated with a post shown in the results of View 1. There can only be one result in View 1. Is this correct?

How is View 1 filtered - by the User with a custom search form, or are the Query Filters predefined in View 1 editor?

Can the post in View 1 have more than one taxonomy term applied?

#806231

Now I want view 2 to match view 1 by that taxonomy.
If I understand you correctly, it sounds like you want to filter View 2 by the taxonomy terms associated with a post shown in the results of View 1. There can only be one result in View 1. Is this correct?

Yes, it's correct.

How is View 1 filtered - by the User with a custom search form, or are the Query Filters predefined in View 1 editor?

It's predefined in View 1 editor, " Author same as inlogged user"

Can the post in View 1 have more than one taxonomy term applied?

Yes, its possible.

#806557
view-2-filter.png

Okay thanks. Here's how I would accomplish this.
- 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
- Now we need to replace the real term slug with the results of View #3. So insert View #3 shortcode inside the taxonomy shortcode attribute for View #2.
- Finally, 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.

Let me know if you run into problems.

#848562

Hi Christian

Place this View #3 just before View #2 on the same page. The results should look like this: slug1, slug2, slug3

- Are the slugs supposed to be shown in shortcode? Do you have an image of the shortcode?

Tobias

#848855
step1.png
step2.png
step3.png

The wpv-post-taxonomy shortcode looks like this when you insert it in the View:

[wpv-post-taxonomy type="book-tax" format="slug"]

You can see how to insert this shortcode in the screenshots attached here. In my example the taxonomy is called "Book Taxes" but yours will be something different.

#881282

It work's!

Perfect, thank you Christian.