Adding custom post types to live search results

Posted in: Newspaper
Post count: 34

Hi,
I was able to add custom post types to the search results by adding a code to the themes function.php

function search_filter($query) {

  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set('post_type', array( 'post', 'page', 'at_biz_dir', 'mdb_item', 'mdb_person', 'mdb_series', 'mdb_company' ) );
    }
  }
}
add_action('pre_get_posts','search_filter');

This works fine and the desired results are displayed on the search results page.

Unfortunately they don’t appear in the live search results, when typing the term into the search field.

How can I add the custom post types to the live search results?

UPDATE: I see that the live results are using the ‘td_module_mx2’ template. Is it possible to overwrite it with a custom template, which I can store in the child theme directory?

  • This topic was modified 6 years by Maddest.
  • This topic was modified 6 years by Maddest.
Post count: 34

UPDATE: I’ve managed to solve the problem, by registering my CPT in the functions.php. Now the custom posts appear in the live results.

However, these custom post types are using attached thumbnails from external directories. They don’t show up in the result (which is not a surprise).
I see that the live results are using the ‘td_module_mx2’ template. Is it possible to overwrite it with a custom template, which I can store in the child theme directory? In this case I could use my own get image functions…

Post count: 18283

Hello !

Yes, you can use this in Child Theme. Please make sure you follow our tutorial closely so you can set up your child theme properly: https://forum.tagdiv.com/the-child-theme-support-tutorial/

Thank you !

Post count: 34

Hi Vlad,

thanks a lot. That works pretty well.

However, is there a way to access the wordpress loop function inside the td_module_mx2 template? E.g. the function get_the_ID() and more?

Thanks a lot

Post count: 18283

Hello !

The main td_module is this file right here: https://prnt.sc/quxvrw

I hope this was helpful !

Thank you !

Post count: 15

I have the same problem but with custom field inclusion in live search results.
How to manipulate query of this widget?

Post count: 35449

Hi creativit,
If you are using our Newspaper theme then you can you 2 types of live search, the one from the tagDiv Cloud Library that can be edited with tagDiv Composer, for this the files responsive for the live search is this one wp-content/plugins/td-cloud-library/shortcodes/header/tdb_header_search.php
Now if you are using a standard header and not a cloud then, the live search will be on a different path, each standard header has a different path for example -> https://i.imgur.com/sJZQZI8.png now the code will be executed in javascript on this file -> wp-content/themes/Newspaper/includes/js/tagdiv-theme.js -> https://i.imgur.com/uq8v79k.png
I hope this will help you!
Thank you!

Viewing 7 posts - 1 through 7 (of 7 total)
The forum ‘Newspaper’ is closed to new topics and replies.