0

There is a plugin that adds a custom post type called "magicai-documents". I want to disable the single view, search, and archive pages (every area of frontend) for an already-built custom post type.

I know I can use 'publicly_queryable' => false when creating a custom post type, but since this custom post type is created by a plugin, I can't update the register_post_type code. What is the best way to disable the single view, search, and archive pages for an already built custom post type?

2
  • I posted an answer, but I actually wanted to ask, what plugin is it and did it not provide a UI/setting which allows you to change the post type args?
    – Sally CJ
    Commented Feb 22 at 15:10
  • 1
    @SallyCJ Your solution is worked. Thanks. The plugin name is "MagicAI for WordPress"
    – Ranuka
    Commented Mar 6 at 5:53

1 Answer 1

1

this custom post type is created by a plugin, I can't update the register_post_type code

You can use either the register_post_type_args or register_<post type>_post_type_args hook to filter the post type args like the publicly_queryable in your case. See an example below which uses the former hook, where I also set exclude_from_search to true to exclude posts with the magicai-documents post type from front end search results.

<?php
add_filter( 'register_post_type_args', 'my_filter_post_type_args', 10, 2 );
function my_filter_post_type_args( $args, $post_type ) {
    if ( 'magicai-documents' === $post_type ) {
        $args['publicly_queryable']  = false;
        $args['exclude_from_search'] = true;
    }

    return $args;
}

If the post type should only be made public on the admin side, then you can set the public arg to false, and then set show_ui and show_in_nav_menus to true.

Alternatively, you could hook on an action like template_redirect and do a conditional tag check like if ( is_singular( 'magicai-documents' ) || is_post_type_archive( 'magicai-documents' ) ) { display a 404 error or (do) something else }, and/or you could filter the WP_Query args using the pre_get_posts action, but changing the appropriate post type args is much easier.

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.