Skip Navigation

[Resolved] WordPress default search including custom post types

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

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 1 reply, has 2 voices.

Last updated by Beda 6 years, 10 months ago.

Assisted by: Beda.

Author
Posts
#526443
Screen Shot 2017-05-20 at 2.16.49 PM.png

Dear Sir/Madam,

By default, only the WP posts will be searched, I have a custom post type, how can I include the custom post in the search result? My custom post type is named Merchants and slug is merchant

I write a code

function my_search_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set('post_type', array( 'post', 'merchant' ) );
    }
  }
}
add_action('pre_get_posts','my_search_filter');

What is the post_type keyword I should add into the $query->set ?

#526485

The Post Type is the Slug of the Post Type, as seen in Toolset > Post Types > your_post_type > Edit > Slug.

Anyway you do not need to code PHP with Toolset.
YOu can simply head to Toolset > Post Types > your_post_type > Edit > Options and make sure that "exclude_from_search" is not checked, and "publicly_queryable" is checked.

The Native WordPress Search results will then include this Post Type.
The Native WordPress Search is like "hidden link"
This search will return also results of Custom Posts created with Types.

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