0

I have a CPT called "participants". When I visit it in the dashboard and search for one of the posts belonging to this CPT (I search a work which is part of the title of an existing post), WP doesn't find anything, although the post is right in the list, containing the work I'm searching. Why the search function doesn't work in this case?

Here's how I created my 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' );

Please note that I altered search function using this code in functions.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;
};
4
  • What "Dashboard Search"? I do not see a search on the dashboard of my install.
    – s_ha_dum
    May 16, 2013 at 14:15
  • In my "Participants" CTP in the dashboard, I have "search Participants" top right. That's what I'm refering to.
    – drake035
    May 16, 2013 at 15:35
  • The "Dashboard" is a particular page-- first link at the top left-- in the WordPress backend, not the whole thing. You are on a post index/edit page. How have you created that CPT? Have you altered the search query at all, for anything?
    – s_ha_dum
    May 16, 2013 at 15:40
  • Thanks for the correction, I meant post edit page indeed. I edited the question to include code for CPT creation and how I modified search function. I see that removing this code solves the problem, so thanks s_ha_dum ! One question though: how could I keep this code (its purpose if for the front end) while allowing search function to work well in the back end with my CPTs?
    – drake035
    May 17, 2013 at 14:36

1 Answer 1

1

Your issue is with the modification to the site search. You need to restrict your filter more. To prevent it executing on the backend add a negated is_admin() condition.

add_filter( 'pre_get_posts', 'tgm_cpt_search' );
function tgm_cpt_search( $query ) {
    if ( !is_admin() && $query->is_search )
    $query->set( 'post_type', array( 'page') );
    return $query;
};

That filter is still pretty aggressive. It will run on every search on the front end, even, for example, a specialized search added by a plugin. You probably want to at least consider adding an is_main_query condition as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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