Skip Navigation

[Resolved] How to reuse a view and change its parameter in the shortcode?

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

Problem:
How to filter view with multiple taxonomy terms using view's shortcode attribute and reuse it

Solution:
You can filter the view by view's shortcode attribute by adding your custom attribute to view's shortcode and filter your view by adding taxonomy filter using view's shortcode attribute:

[wpv-view name="view-1" wpvresearcher="Atef Wagih,Emad Boules" operator="AND"]

You can find proposed solution with the following reply:
=> https://toolset.com/forums/topic/how-to-reuse-a-view-and-change-its-parameter-in-the-shortcode/#post-601059

Relevant Documentation:
=> https://toolset.com/documentation/user-guides/passing-arguments-to-views/#examples
=> https://toolset.com/documentation/user-guides/filtering-views-by-taxonomy/

This support ticket is created 6 years, 10 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by AtefR7377 6 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#600592

Hi,
I have a very big website that I built using Divi theme and a plugin called "Content views Pro".

I am migrating the website to the WP Types system along with GeneratPress theme.

The plugin provides a shortcode with parameters were you can build a view (e.g. grid), then insert it in a post/page.

However, you can control the paramters, for example:
[pt_view id="62645c2sg6" taxonomy=person terms="authors,singers" operator="AND" taxonomy2=category terms2="Retired" operator2="NOT IN"]

this displays a grid of posts that are present as assigned to 2 custom taxonomies called "authors" and "singers". however, the posts have to be NOT assigned to the category called "retired".

I searched a lot in the Views documentation and API, but could not find a similar function.

It will be impossible for me to keep creating single use views because this will be an endless task, and the "Fields and Views" button will be unusable.

can you please advise if there is a way to control the views without having to keep creating single use views?

thanks,

#600714

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I see that you want to pass the parameters shortcode.

You can also pass arguments to view's shortcode.
=> https://toolset.com/documentation/user-guides/passing-arguments-to-views/#examples

You should filter your view by adding query filter to filter your view by taxonomy and filter it by shortcode attribute:
=> https://toolset.com/documentation/user-guides/filtering-views-by-taxonomy/

Please let me know if you need further assistance to setup such view.

#600722

Hi,
thanks a lot for the quick response. I followed the steops and it worked for one taxonomy term, whether by name or slug:

[wpv-view name="argument-1" wpvresearcher="emad-boules"]
[wpv-view name="argument-1" wpvresearcher="Atef Wagih"]

however, when I foloowed the documentation to add an operator, it did not work and the view had all posts. it should have shown only one post, because i added the 2 researchers to only one post.
here is what i used:

[wpv-view name="argument-1" wpvresearcher="Atef Wagih,Emad Boules" operator="AND"]

this is in relation to this page:
https://toolset.com/documentation/user-guides/filtering-views-by-taxonomy/
and this title: Value set by View shortcode attribute

can you please help?
thanks a lot

#600724

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - view's offers filter to customize view query on fly. You can use wpv_filter_query to customize your query on fly and as per your need.

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example:

add_filter( 'wpv_filter_query', 'exclude_terms_func', 10, 3 );
function exclude_terms_func($query, $setting, $views_ID)
{
    if($views_ID == 52) {
 
        $query['tax_query'][] = array( 
             array(
            'taxonomy' => 'your-taxonomy-slug',
            'field' => 'slug',
            'terms' => array( 'some'),
            'operator' => 'IN'
        ),
            array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => array( 'tst'),
            'operator' => 'NOT IN'));
        $query['tax_query']['relation'] = 'AND';
    }
    return $query;
}

-- You should adjust your original view ID and your taxonomy slug and terms as needed and code as needed.

See this related ticket:
=> https://toolset.com/forums/topic/parametric-search-filter-taxonomy-value-set-by-the-current-page-2/#post-371509

#600903

Hi Minesh,
Thanks for the code.
However, is this function needed to be pasted in the functions.php? if so, then I have literally several hundred pages that need this view arguments. I can't add a function in the functions.php for every page, otherwise I will end up with an unusable site.
Is there a way to add the "in", "Not in" and "and" to the shortcode like this:

[wpv-view name="argument-1" wpvresearcher="Atef Wagih,Emad Boules" operator="AND"]

thanks,

#601059

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - you do not need to add operator="AND" as you can set up the logical operator from your taxonomy filter.

I think what you need to do is setup your taxonomy filters from view's query filter section filter by shortcode attribute and select your desired operation relationship from the dropdown I marked with red color box with the following screenshot.
=> hidden link

And then, you need to just pass your shortcode attribute values.

#601204

Hi Minesh,
thanks for the solution. it worked perfectly.
the reason it was not working was that the query filter in the view was chosen as "any". when i changed it to "And" it worked perfectly.

the implication of this is that if i want a view for "any" and another view for "and", i need to create 2 views.

thanks a lot.