Site logo

Archived topic

Disable search for custom post type

5 replies · Started by Hilton on January 2, 2022

Viewing posts 1–6 of 6

Hi,

I have some problems I hope you can help.

I used the function below to create the CPT video:

function mefvideos_init() {
    // set up video labels
    $labels = array(
        'name' => 'Vídeos',
        'singular_name' => 'Vídeo',
        'add_new' => 'Adicionar novo',
        'add_new_item' => 'Adicionar novo',
        'edit_item' => 'Editar vídeo',
        'new_item' => 'Novo vídeo',
        'all_items' => 'Todos os vídeos',
        'view_item' => 'Ver vídeo',
        'search_items' => 'Buscar vídeos',
        'not_found' =>  'Nenhum vídeo encontrado',
        'not_found_in_trash' => 'Nenhum vídeo na lixeira', 
        'parent_item_colon' => '',
        'menu_name' => 'Vídeos',
    );
    
    // register post type
    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'taxonomies' => array( 'videos_category' ),
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'exclude_from_search' => true,
        'rewrite' => array('slug' => 'video', 'with_front' => FALSE),
        'query_var' => true,
        'menu_icon' => 'dashicons-video-alt3',
        'show_in_rest' => true, // To use Gutenberg editor.
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes'
        )
    );
    register_post_type( 'videos', $args );
    
    // register taxonomy
    register_taxonomy('videos_category', 'videos', array('with_front' => true, 'hierarchical' => true, 'label' => 'Categorias vídeos', 'show_in_rest' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'videos' )));
}
add_action( 'init', 'mefvideos_init' );

I used this other function so that any page, post, tag or category created would not cause problems with the CPT "video":

dd_filter( 'wp_unique_post_slug_is_bad_hierarchical_slug', 'fe_prevent_slug_conflict', 10, 4 );
add_filter( 'wp_unique_post_slug_is_bad_flat_slug', 'fe_prevent_slug_conflict', 10, 3 );
function fe_prevent_slug_conflict( $is_bad_slug, $slug, $post_type, $post_parent_id = 0 ) {

	$reserved_top_level_slugs = apply_filters(
		'fe_reserved_top_level_slugs',
		array('videos',)
	);

	if (
		// Only check top level post slugs (i.e. not child posts).
		0 === $post_parent_id
		&& in_array( $slug, $reserved_top_level_slugs )
	) {
		$is_bad_slug = true;
	}
	return $is_bad_slug;
}

And finally, I used this function to enable the creation of categories for the CPT:

function wckc_add_my_custom_post_type ($query) {
    if(
        empty($query->query['post_type'])
        or $query->query['post_type'] === 'post'
    ){
        $query->set('post_type', array('post', 'page', 'videos'));
    }
}

add_action('pre_get_posts', 'wckc_add_my_custom_post_type');

This is my CPT https://bit.ly/31mL9JQ main page, this is a single page https://bit.ly/3zjpf6B and this is a category page https://bit.ly/31knwSb

Problems:

1) CPT are displaying in the search results, and would not like to enable them. Even because I used the parameter 'exclude_from_search' => true and with that they shouldn't be listed in the search.

2) In Wordpress I can no longer do any search for posts because when I try I get the message "Invalid Post Type" https://bit.ly/3EPAWmS

Thanks

Hi there,

not sure on this.
But on this snippet:

I used this other function so that any page, post, tag or category created would not cause problems with the CPT “video”:

Your first line is incorrect:
dd_filter( 'wp_unique_post_slug_is_bad_hierarchical_slug', 'fe_prevent_slug_conflict', 10, 4 );

Its missing the a from add_filter

And the snippet below:

And finally, I used this function to enable the creation of categories for the CPT:

is a pre_get post filter, i am not sure how that relates to the creating categories. What happens if you remove that snippet as that is most likely causing the issue with No Posts being found.

Hi David, the problem is that if I remove this snippet the CPT categories are not displayed, as you can see here on the dev site https://bit.ly/3pOIamF even though the single post page appears correctly https://bit.ly/3zkFWyE

Try changing it to:

function wckc_add_my_custom_post_type ($query) {
    
    if( is_admin() && is_search() ) {
	return;
    }

    if( empty($query->query['post_type']) || $query->query['post_type'] === 'post' ){
        $query->set('post_type', array('post', 'page', 'videos'));
    }
}

add_action('pre_get_posts', 'wckc_add_my_custom_post_type');

This will simply return the defaults if in admin.

Hi David, This solution worked well to show CPT categories (single: https://bit.ly/3zkFWyE / archive: https://bit.ly/3pOIamF )

Now do you know why CTPs are showing up in searches if we are using exclude_from_search => true ?

Thanks!

This archived topic is closed to new replies.