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 |2 Answers
Reset to default 2Finally 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
版权声明:本文标题:Custom Post type doesn't appear in search results 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310953a1934564.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pre_get_posts
for a post type to appear in search. Are you certain the posts exist? Try settingshow_ui
andshow_in_menu
totrue
temporarily, so you can see if the posts appear in the back-end. – Jacob Peattie Commented Jun 18, 2019 at 10:46