[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:
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?
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:
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:
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.
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.