我有一个名为“参与者”的CPT。当我在仪表板中访问它并搜索属于此CPT的一篇帖子(我搜索的作品是现有帖子标题的一部分)时,WP没有找到任何东西,尽管帖子在列表中,包含我正在搜索的作品。为什么在这种情况下搜索功能不起作用?
以下是我创建CPT的方式:
function my_custom_post_participant() {
$labels = array(
\'name\' => _x( \'Participants\', \'post type general name\' ),
\'singular_name\' => _x( \'Participant\', \'post type singular name\' ),
\'add_new\' => _x( \'Add New\', \'participant\' ),
\'add_new_item\' => __( \'Add New Participant\' ),
\'edit_item\' => __( \'Edit Participant\' ),
\'new_item\' => __( \'New Participant\' ),
\'all_items\' => __( \'All Participants\' ),
\'view_item\' => __( \'View Participant\' ),
\'search_items\' => __( \'Search Participants\' ),
\'not_found\' => __( \'No participants found\' ),
\'not_found_in_trash\' => __( \'No participants found in the Trash\' ),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Participants\'
);
$args = array(
\'labels\' => $labels,
\'description\' => \'Holds our participants and participant specific data\',
\'public\' => true,
\'menu_position\' => 4,
\'supports\' => array( \'title\', \'custom-fields\' ),
\'has_archive\' => true,
);
register_post_type( \'participant\', $args );
}
add_action( \'init\', \'my_custom_post_participant\' );
function my_updated_messages_participant( $messages ) {
global $post, $post_ID;
$messages[\'participant\'] = array(
0 => \'\',
1 => sprintf( __(\'Participant updated. <a href="%s">View participant</a>\'), esc_url( get_permalink($post_ID) ) ),
2 => __(\'Custom field updated.\'),
3 => __(\'Custom field deleted.\'),
4 => __(\'Participant updated.\'),
5 => isset($_GET[\'revision\']) ? sprintf( __(\'Participant restored to revision from %s\'), wp_post_revision_title( (int) $_GET[\'revision\'], false ) ) : false,
6 => sprintf( __(\'Participant published. <a href="%s">View participant</a>\'), esc_url( get_permalink($post_ID) ) ),
7 => __(\'Participant saved.\'),
8 => sprintf( __(\'Participant submitted. <a target="_blank" href="%s">Preview participant</a>\'), esc_url( add_query_arg( \'preview\', \'true\', get_permalink($post_ID) ) ) ),
9 => sprintf( __(\'Participant scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview participant</a>\'), date_i18n( __( \'M j, Y @ G:i\' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __(\'Participant draft updated. <a target="_blank" href="%s">Preview participant</a>\'), esc_url( add_query_arg( \'preview\', \'true\', get_permalink($post_ID) ) ) ),
);
return $messages;
}
add_filter( \'post_updated_messages\', \'my_updated_messages_participant\' );
请注意,我在函数中使用此代码更改了搜索函数。php:
add_filter( \'pre_get_posts\', \'tgm_cpt_search\' );
function tgm_cpt_search( $query ) {
if ( $query->is_search )
$query->set( \'post_type\', array( \'page\') );
return $query;
};