admin管理员组

文章数量:1122846

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();
  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?

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?

Share Improve this question edited Jun 19, 2019 at 1:29 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jun 18, 2019 at 8:07 user170197user170197 212 bronze badges 4
  • 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 Commented Jun 18, 2019 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 Commented Jun 18, 2019 at 11:44
  • What does your search.php template look like? – Jacob Peattie Commented Jun 18, 2019 at 11:50
  • The normal twentyseventeen template. – user170197 Commented Jun 18, 2019 at 12:13
Add a comment  | 

2 Answers 2

Reset to default 2

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.

I have run into a similar issue and your comment saved me! I understand that it was Polylang!

Did you find a way to fix your issue without disabling Polylang completely? I would love to keep using the plugin, but need CPT too.

Thanks a lot :)

本文标签: Custom Post type doesn39t appear in search results