- This topic has 5 replies, 2 voices, and was last updated 2 years, 3 months ago by David.
-
AuthorPosts
-
January 2, 2022 at 8:47 pm #2066972Hilton
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
January 3, 2022 at 5:38 am #2067337DavidStaffCustomer SupportHi 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
fromadd_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.
January 3, 2022 at 6:28 am #2067388HiltonHi 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
January 3, 2022 at 9:22 am #2067759DavidStaffCustomer SupportTry 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.
January 3, 2022 at 10:11 am #2067808HiltonHi 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!
January 3, 2022 at 11:10 am #2067876DavidStaffCustomer SupportI edited my snippet above:
https://generatepress.com/forums/topic/disable-search-for-custom-post-type/#post-2067759
This will exclude the post types from the search results which i think is overriding your exclude_from_search arg.
-
AuthorPosts
- You must be logged in to reply to this topic.