{"id":15394,"date":"2026-10-15T12:13:25","date_gmt":"2026-10-15T09:13:25","guid":{"rendered":"https:\/\/creativethemes.com\/blocksy\/?post_type=documentation&#038;p=15394"},"modified":"2026-10-15T12:13:25","modified_gmt":"2026-10-15T09:13:25","slug":"restrict-404-no-results-search-form-to-specific-post-types","status":"publish","type":"documentation","link":"https:\/\/creativethemes.com\/blocksy\/docs\/code-snippets\/restrict-404-no-results-search-form-to-specific-post-types\/","title":{"rendered":"Restrict 404\/No Results Search Form to Specific Post Types"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Use the following snippet to restrict the default search form displayed on <strong>404<\/strong> and <strong>search results<\/strong> pages to specific post types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, searches are limited to regular <strong>posts<\/strong> and <strong>pages<\/strong>. You can replace <code>post<\/code> and <code>page<\/code> with any registered post types that you want to include.<\/p>\n\n\n<div class=\"ct-code-box wp-block-blocksy-landing-child-code-box\" data-type=\"php\">\n\t<div class=\"ct-code-box__header\">\n\t\t<div class=\"ct-code-box__meta\">\n\t\t\t<span class=\"ct-code-box__label\"><svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\"><path d=\"m9 8-4 4 4 4\"\/><path d=\"m15 8 4 4-4 4\"\/><\/svg>PHP<\/span>\n\t\t\t\t\t<\/div>\n\t\t<button type=\"button\" class=\"ct-code-box__copy\" aria-label=\"Copy code\">\n\t\t\t<svg class=\"ct-code-box__icon-copy\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.8\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\"><rect x=\"9\" y=\"9\" width=\"11\" height=\"11\" rx=\"2\"\/><path d=\"M5 15V6a2 2 0 0 1 2-2h9\"\/><\/svg>\n\t\t\t<svg class=\"ct-code-box__icon-check\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2.2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\"><path d=\"M5 12.5l4.5 4.5L19 7.5\"\/><\/svg>\n\t\t\t<span class=\"ct-code-box__copy-label\">Copy<\/span>\n\t\t<\/button>\n\t<\/div>\n\t<pre class=\"ct-code-box__code\"><code>add_filter(&#039;search_form_args&#039;, function ($args) {\n    if (\n        (is_404() || is_search())\n        &amp;&amp; ! isset($args&#91;&#039;ct_post_type&#039;])\n    ) {\n        $args&#91;&#039;ct_post_type&#039;] = &#91;&#039;post&#039;, &#039;page&#039;];\n    }\n\n    return $args;\n});<\/code><\/pre>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The snippet only changes forms that do not already define a <code>ct_post_type<\/code> value. Search forms that explicitly specify their own post types remain unchanged.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use the following snippet to restrict the default search form displayed on 404 and search results pages to specific post types. In this example, searches are limited to regular posts and pages. You can replace post and page with any registered post types that you want to include. The snippet only changes forms that do [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"footnotes":""},"doc_categories":[125],"class_list":["post-15394","documentation","type-documentation","status-publish","hentry","doc_categories-code-snippets"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"acf":[],"_links":{"self":[{"href":"https:\/\/creativethemes.com\/blocksy\/wp-json\/wp\/v2\/documentation\/15394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/creativethemes.com\/blocksy\/wp-json\/wp\/v2\/documentation"}],"about":[{"href":"https:\/\/creativethemes.com\/blocksy\/wp-json\/wp\/v2\/types\/documentation"}],"author":[{"embeddable":true,"href":"https:\/\/creativethemes.com\/blocksy\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/creativethemes.com\/blocksy\/wp-json\/wp\/v2\/comments?post=15394"}],"version-history":[{"count":1,"href":"https:\/\/creativethemes.com\/blocksy\/wp-json\/wp\/v2\/documentation\/15394\/revisions"}],"predecessor-version":[{"id":15395,"href":"https:\/\/creativethemes.com\/blocksy\/wp-json\/wp\/v2\/documentation\/15394\/revisions\/15395"}],"wp:attachment":[{"href":"https:\/\/creativethemes.com\/blocksy\/wp-json\/wp\/v2\/media?parent=15394"}],"wp:term":[{"taxonomy":"doc_categories","embeddable":true,"href":"https:\/\/creativethemes.com\/blocksy\/wp-json\/wp\/v2\/doc_categories?post=15394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}