1

Is it possible to create a search page that will search for a keyword only in a custom post type that has the status "draft?"

Basically, I have a lot of data I was planning on importing as a custom post type. The data would be simple, "name", "answer", "notes." However, I don't want this data published because each custom post would have almost no data and the site would then have 5,000 spammy pages.

However, users frequently want to know the "answer" for certain "names," and I need to give them the ability to search and find.

2

It's possible to search within drafts by setting the post_status argument to draft in your search query, however, a better option is to control things with the arguments passed to register_post_type.

We first set public to false, which will hide the post type everywhere- front and back end. We then selectively enable show_ui to get the admin UI, and set exclude_from_search to false so they show up in front end searches. We also set rewrite to false, so WordPress doesn't generate rewrite rules for this post type.

You will then have published posts that are searchable, but they will have no individual pages on the front end.

$args = array(
    'public' => false,
    'show_ui' => true,
    'exclude_from_search' => false,
    'rewrite' => false,
    'label' => 'Name',
    // your other arguments...
);
register_post_type( 'name', $args );
  • This looks like an awesome answer! Thanks! Do you know of a way to debug why the post type is still visible even though 'public' => false? – jonbon May 24 '17 at 4:12
  • I forgot one other argument- rewrite should be false so those pages 404 instead of the strange side-effect of showing the main blog page. If the post type was already registered, visit the Settings > Permalinks page to flush the rewrite rules cache. – Milo May 24 '17 at 4:24
  • I'm also stupid, I had a huge list of arguments and 'publicly_queryable' => true was one of them :) -- works now – jonbon May 24 '17 at 4:26
  • So, when the hidden post is returned in a search by default the result links to /?custom_posttype=slug-here. Is there a way to only allow these posts to show up on a custom search page? – jonbon May 24 '17 at 5:38
  • You can set exclude from search to true and still query for that post type in your own custom search. – Milo May 24 '17 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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