Skip Navigation

[Resolved] displaying view results based on category in a shortcode

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

Supporter timezone: Europe/London (GMT+01:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by joshM-4 7 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#449887

I am trying to: customize a shortcode for a view so that it only shows the relevant posts for a specified category/taxonomy

I visited this URL:

I expected to see:

Instead, I got:

----

I'm building a site with a post type (Deal Stories) that can fall into one of 3 categories. Each of those categories has a custom page I've designed. At the bottom of those pages, I'm wanting to insert a shortcode that will show the most recent (Deal Story) that falls within that category. Is there a way to specify which category to show in the shortcode?

Here is what I've tried most recently but it's not working. It shows only the most recent posting, not by category: [wpv-view name="view-for-deal-stories-list" view_display="layout" limit="1" orderby="taxonomy-management"] (Management is the category for that page).

I do realize I could create separate Views for each category, but that's a lot of extra stuff to maintain and I thought it there was a way to do this it would be more efficient.

Thanks in advance for taking a look!

#450052

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Josh

You can pass arguments to a view either by url paramater or, in your case, via shortcode attribute, and then your view can filter by the passed argument.

It is described on the following page and covers your example, but if you have problems with it let me know:

https://toolset.com/documentation/user-guides/passing-arguments-to-views/

#450212

Thanks for the link, Nigel!

That helped and I feel like I'm understanding it but something isn't working quite right and I don't know what I'm missing.

So I have parametric search set up for the Deal Categories - I went to that page and sorted by one of the categories (Consulting) and the url updated like this

.../wp-admin/admin-ajax.php?wpv-deal-category=consulting&wpv_aux_current_post_id=6...

So I see the "wpv-deal-category=consulting" and add that in to the shortcode, putting "consulting" in quotes - here's the full shortcode I ended up with:

[wpv-view name="view-for-deal-stories-list" view_display="layout" limit="1" wpv-deal-category="consulting"]

But it's still not working :/. It seems to me that it should be using orderby though? Right? I don't know...

Any additional help you can offer would be much appreciated!

#450228

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Josh

From your original question you say you want to add a view to the bottom of your pages which shows the most recent post in the relevant category. In your last reply you mention a parametric search view. Is that used somewhere else, because the view at the bottom of your page is just a normal view that returns a single result with a query for the category which is passed to it via shortcode attribute.

From that you should infer that there are two parts to the solution.

Passing a value to the view, and then using that value to filter the results.

You pass a value to the view using a shortcode attribute. You can call that attribute whatever you want, and to illustrate the point let's say you are going to use an attribute called 'benedict'.

So if you are on a page where you want your view to show the most recent post with the category "consulting" then your shortcode will look something like this:

[wpv-view name="view-for-deal-stories-list" view_display="layout" limit="1" benedict="consulting"]

So now in the view itself we can add a filter for the category and take the actual value to filter by from our shortcode attribute 'benedict'.

See the screenshot to see me adding such a filter query. You'll note that the normal shortcode attribute name for a category would be 'wpvcategory' but I changed it to 'benedict' in this example to demonstrate that it is simply a variable name which can be whatever you want as long as the name of the attribute being passed is the same as the name being used in the filter. It doesn't have any special significance, but you will probably want to leave it at the default wpvcategory (being sure to use same in your shortcode).

By the way, WordPress doesn't allow you to order by taxonomies, you would need a custom solution for that.

#450229

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screen Shot 2016-10-25 at 16.09.09.png

Sorry Josh,

I missed the screenshot, which is attached here.

Also, I meant to say that in the example wpv-view shortcode above I have done the same as you and manually added 'consulting', but depending on where you are inserting that shortcode you may be able to use a shortcode to generate the taxonomy term.

See the views shortcodes listed here: https://toolset.com/documentation/user-guides/views-shortcodes/

#450245

Thank you Nigel, and I apologize for the confusion of the parametric search - I didn't provide clear context to why I was there.

After much comparing of your descriptions and screenshot (helpful - thank you), I realized that my problem was trying to use the shortcodes when the filter was "set by one URL parameter" instead of "set by one View shortcode attribute". Once I changed that I was able to filter the way I needed to.

Thanks again for your help and patience!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.