Custom Post type doesn't appear in search results

by user170197   Last Updated June 18, 2019 08:08 AM

I am working on a plugin where I am adding a custom post type and inserting its posts based on wp_insert_post function once the plugin is activated. The problem is that the custom post types cannot appear on search whatever I do.

I have tried to include the new custom post type in the post types to be searched through the pre_get_posts hook. I have even checked it with relevanssi plugin and made sure they are indexed and printed the arrays of custom post types to be indexed and it is already shown in the array. I have tried to insert it manually through the admin interface. I have even tried the solution mentioned in this question and none of the above worked:

Here is my code regarding registering the custom post type:

    $args = array(
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor' ),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => false,
    'show_in_menu'          => false,
    'menu_position'         => 5,
    'show_in_admin_bar'     => false,
    'show_in_nav_menus'     => false,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'capability_type'       => 'post',
register_post_type( 'taxarchive', $args );

This is the code for inserting the posts:

$taxonomies = $wpdb->get_results("SELECT term_id, name FROM $wpdb->terms");
  $taxonomies_array = array();
foreach ($taxonomies as $taxonomy) {
    $term = get_term($taxonomy->term_id);
    $taxonomy = get_taxonomy($term->taxonomy);
    if(isset($term->taxonomy) && $taxonomy->publicly_queryable) {
    $taxonomies_array[$taxonomy->name] = $term->taxonomy;
    $term_post = array(
      'post_title'    => $term->name,
      'post_content'  => $term->name,
      'post_status'   => 'publish',
      'post_type'     => 'taxarchive'
    if(post_exists($term->name) == 0) {



This is the code adding the taxarchive post type to the pre_get_posts hook:

    function add_post_type_to_search( $query ) {
    if ( is_admin() || ! $query->is_main_query() ) {

  $args = array(
     'exclude_from_search'   => false,

  $post_types = get_post_types( $args, 'names', 'and' );

        'post_type', array_values($post_types)
//    print_r($query);

add_filter( 'pre_get_posts', 'add_post_type_to_search' );

I never get any of the taxarchive post type posts in the search results when I search for any related keyword. How can they be indexed in the results?

Related Questions

Limit search form to 4 custom post types only

Updated November 21, 2017 22:08 PM

Disappearing posts in search results

Updated April 03, 2017 11:08 AM

Hiding Custom Post Type from Google?

Updated September 28, 2017 15:08 PM