0

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:

https://stackoverflow.com/questions/36787961/wordpress-custom-post-type-not-showing-in-search-results

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();
  wp_defer_term_counting(true);
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) {
    wp_insert_post($term_post);
  }

    }
  }

  wp_defer_term_counting(false);

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() ) {
        return;
    }

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

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


    $query->set(
        '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?

  • You shouldn't need to filter pre_get_posts for a post type to appear in search. Are you certain the posts exist? Try setting show_ui and show_in_menu to true temporarily, so you can see if the posts appear in the back-end. – Jacob Peattie Jun 18 at 10:46
  • The pre_get_posts filter was added to make sure the post type is considered. May be I don't need it, but I added it when I didn't see it indexed. I also tried to show it in the back-end through the show_ui and show_in_menu and they appear in the back-end and their permalink is valid. – user170197 Jun 18 at 11:44
  • What does your search.php template look like? – Jacob Peattie Jun 18 at 11:50
  • The normal twentyseventeen template. – user170197 Jun 18 at 12:13
0

Finally I found the cause of this issue. It was simply because of Polylang plugin. As the plugin adds a taxonomy related to the language of the website to all posts, it adds a LEFT JOIN statement to the search query with a condition WHERE 1=1 AND post has that taxonomy id. As I didn't add this taxonomy in the wp_insert_post function, none of these posts related to the post type I created can meet the condition in the query added by Polylang. Once I deactivated it, everything worked fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.