Skip Navigation

[Resolved] Filterd by post type in views or archives

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 3 replies, has 1 voice.

Last updated by willaL 1 month, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2803267
custom field setting.PNG

Greetings. I noticed that currently toolset does not support filtering by post type by default. So I want to create a feature that uses the post type's custom field to filter specific post types. To achieve this, I need to synchronize the post type into a custom field. The post types include product, artwork, and creator. Since the post type does not change, I would like the synchronization to happen during the initial creation of the post, avoiding repeated execution during subsequent saves or updates.

I have attempted to use the save_post hook to synchronize the post type data of already created posts into a custom field. However, the code did not work during testing. I suspect the issue might be related to configuration problems or the failure to update the custom field properly.

function func_set_custom_field_post_type( $post_id, $post ) {
if ( in_array( $post->post_type, ['product', 'artwork', 'creator'] ) ) {
update_post_meta( $post_id, 'wpcf-post-type', $post->post_type );
}
}
add_action( 'save_post', 'func_set_custom_field_post_type', 999, 2 );

Do you have any better suggestions for implementing this feature? I would appreciate more specific solutions to address this problem. Thank you!

Reference: https://toolset.com/forums/topic/frontend-search-filter-by-post-types/

#2803340

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

As I understand - you want to assign/setup custom field value for the custom field "wpcf-post-type" for the existing posts? if that correc?
OR
your aim is to setup custom field value for the custom field "wpcf-post-type" automatically whenever user add/edit the post.

#2803370

Thanks for your response.
Mainly I want to setup custom field value for the custom field "wpcf-post-type" automatically whenever user add/edit the post. However, I do not wish to create many new posts for testing the filter function.
So I need both functions.

#2803372

Minesh
Supporter

Languages: English (English )

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

The code you shared should work without any issue but the glitch I found is with the custom field values you setup.

As per your screenshot:
- https://toolset.com/wp-content/uploads/2025/03/2803267-custom_field_setting_1.png

With "Custom Field Content" column you setup values as Products, Artworks and Creators. You can change the "Dispaly Text" column as Products, Artworks and Creators.

Can you please modify "Custom Field Content" column values as product, artwork and creator and then save your custom field and then try to add new post with correct values and save and try to edit - it should work.

#2803379

Oh silly me! I didn't notice that I misplaced it at all. Thanks for pointing it out!
Now it works perfectly.