Skip Navigation

[Resolved] Filter by post type for archive view

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 8 replies, has 2 voices.

Last updated by ben 1 year, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2515331

ben
screenshot-2.jpg
screenshot-1.jpg

hello,

I am using Toolset products for a quite long while.

here is what i want to do:
I want to add a checkbox form group to filter the posts by post types for my archive view (see screenshot-1.jpg)

I did some researches and found those two info:
https://toolset.com/forums/topic/filter-by-post-type/
https://toolset.com/forums/topic/post-type-filter/

I tried to use the codes and tips in the above links, and the frontend is showing something, but it doesn't work. (see screenshot-2.jpg)

I got stuck there and could you please provide some tips and or articles?

PS: I am using Toolset Types 3.4.17 and Toolset Views 3.6.3

Thanks,
Ben

#2515953

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

There is no native feature to filter posts type post type but as a workaround you can add it and you already followed.

I would like to know that do you have taxonomy archive or post type archive? As I understand you have one taxonomy assigned to different post types and that is why you want to add post type filter - correct?

#2515973

ben
screenshot-6.jpg
screenshot-5.jpg
screenshot-4.jpg
screenshot-3.jpg

hi Minesh,

Thank you for your reply.

I am not sure what is the best way to answer your question, but here is what I have and what I want to do:

I added some tags/terms for the native wordpress post (not the custom post type, just the organic post tag), and I let a few custom post type share that same tag (see screenshot-3.jpg and screenshot-4.jpg)

Then I create a view/template for that tag (see screenshot-5.jpg and screenshot-6.jpg), that view/template is working fine, see hidden link as an example, but I just want to add a checkbox filter to filter different post types.

Please let me know if my expression is not clear

thanks,
Ben

PS:
here is the code for loop of that archive:

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<div class="table-responsive">
<table class="table table-bordered course-table">
<thead>
<tr>
<td>来源</td>
<td>标题</td>
<td>介绍</td>
<td>日期</td>
</tr>
<theader>
<tbody>
<wpv-loop>
<tr>
[wpv-post-body view_template="loop-item-in-generic-tag-archive"]
</tr>
</wpv-loop>
</tbody>
</table>
</div>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

here is the code for loop item:

<td>[wpv-post-type show="single"]</td>
<td class="course-name">
<a href="[wpv-post-url]" target="_blank" rel="noopener">[wpv-post-title]</a><br>
</td>
<td class="course-intro">[wpv-post-excerpt format="noautop"]</td>
<td class="course-date">发布:[wpv-post-date]<br>更新:[wpv-post-date type="modified"]</td>

#2516007

Minesh
Supporter

Languages: English (English )

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

Ok as shared there is no feature to add post type front-end filter to taxonomy archives but we can add a workaround.

Can you please tell me what post types you want to add a post type filter options and share admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. I have set the next reply to private which means only you and I have access to it.

#2516023

ben

oh, forgot one more post type: video

well, I think I can add that by myself if you can make it work.

#2516093

Minesh
Supporter

Languages: English (English )

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

Could you please check now: hidden link

I've added the following filter code to your view's "Search and pagination" section:
=> hidden link

<div class="form-group">
    <label>
[wpml-string context="wpv-views"]Post Type:[/wpml-string]</label>[wpv-control type="checkboxes" values=",post,course,resource,ebook,video" display_values="All,Post,Course,Resource,E-Book,Video" url_param="wpv-post-type"]
</div>

Then - I've added the following code to "Custom Code" section Offered by Toolset:
=> hidden link

add_filter('wpv_filter_register_url_parameters_for_posts', function($attributes, $view_settings){
  
  
    if($view_settings['view_id'] == 4708){
        $attributes[] = array(
            'query_type'=> 'posts',
            'filter_type'=> 'post_type',
            'value'=> 'custom_field_value',
            'attribute'=> 'wpv-post-type',
            'expected'=> 'string',
        );
    }
    return $attributes;
}, 99, 2);


add_action( 'pre_get_posts', 'func_tax_archive_filter_by_posttype',101,1); 
function func_tax_archive_filter_by_posttype($query){

  if ($query->is_tag() && $query->is_main_query() and isset($_GET['wpv-post-type']) ) {
    
  		$query->query_vars['post_type'] = $_GET['wpv-post-type'];
   
      
    
   }
  return $query;
}

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/

You can format the checkboxes as per your requirement. Glad that I can help you.

#2516103

ben

hi Minesh,

Thank you a MILLION for that!

I just check the website and everything is working GREAT and COOL!!!

I will take care of the formatting/styling part.

Well ,I did some browser refresh during the process of you working on that, I noticed that it seemed it's not a easy thing to do, because I could feel you did a lot of attempts and debugs, I am just curious what is the challenging part for you when you were working on that. If it's easy and fast to explain to me, that will be great, but if it's hard to explain, just forget about it.

Thank you again Minesh.

#2516105

Minesh
Supporter

Languages: English (English )

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

Well - I've used "pre_get_posts" hook to filter the view based on post type and another hook "wpv_filter_register_url_parameters_for_posts" is used to register the url param.

#2516113

ben

My issue is resolved now. Thank you!

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