Skip Navigation

[Resolved] Using filter by post in archives and why hiding the url works.

This support ticket is created 5 years, 9 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+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by matthewL-6 5 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#1204389

Hi Amazing Toolset support,

I am creating a bigger and better local business directory. Which has an archive of businesses, attractions events and promotions.

I am using the filter by post type code provided by toolset. (https://toolset.com/forums/topic/filtersearch-by-custom-post-type/)

Here's me code:
add_filter( 'wpv_filter_query', 'post_type_filter_func', 10, 3 );

function post_type_filter_func( $query_args, $settings, $view_id ) {
if ( $view_id == 28 && isset($_GET['wpv-post-type'][0]) && !empty($_GET['wpv-post-type'][0]) ) {
$query_args['post_type'] = $_GET['wpv-post-type'][0];
}
return $query_args;
}

[wpv-filter-controls][wpml-string context="wpv-views"]Post Type:[/wpml-string] [wpv-control url_param="wpv-post-type" type="select" values=",post ,algarve-event ,algarve-promotion ,algarve-attraction ,live-algarve" display_values="All, Business, Event, Promotion, Attraction, Article"]
[wpv-filter-submit output="bootstrap"][/wpv-filter-controls]

1. Why does this code not seem to work on an archive as apposed to a view?

What is the tidiest way to make this an archive?

1, create a view and then display the view within the archive?

2, create a view and add it to a page and redirect the archive?

3, Any other suggestions

What is the link to your site?
hidden link

I look forward to hearing from you.

Thank you in advance, Matt

#1204548

Nigel
Supporter

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

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

Hi Matt

The wpv_query_filter hook doesn't run on archive pages, and we do not in fact offer any such filter to modify the arguments of the main query on an archive page.

To do so you would have to use the WordPress API itself to modify the query on an archive page, using pre_get_posts and WordPress conditional tags.

Here is a post that describes what's involved (you can skip the part about making custom PHP templates, it is the code added to functions.php in the post that is instructive): hidden link

#1204576

Hi,

so to be clear, I can get the archive to display multiple posts types by changing the loop selection but I cannot filter them.

By modifying the query I will also be able to add a filter to the archive? And still use toolset for the layout at the same time.

Thank you

#1204682

Nigel
Supporter

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

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

You can make changes in the GUI to customise the archive, but there are no PHP hooks (from Toolset) available to modify the query in the same way that you can with Views (as per your custom code).

You can't include more than one post type in the archive (you can re-use the same custom archive for different post types).

You can include custom search filters in the GUI just like when editing a View. But filtering by standard fields (rather than custom fields and taxonomies) isn't supported, hence why you had to resort to using a filter hook with a View.

To achieve the same with an archive you have to step outside of Toolset and go directly to the native WordPress API, using pre_get_posts to modify the posts that are returned by an archive query.

You can, yes, create a custom archive with Toolset where you design the output of the archive results with Toolset, but you need to use pre_get_posts to affect what those results are if it is not something you can do from the GUI.

#1204715

My issue is resolved now. Thank you!

I decided it was best to make a view with all of the post types and then also have individual archives.

It is not likely to be a major duplicate content issue as the content will appear in a different order but overall is great for SEO and gives the user the option to choose.