20
20
21
21
use Clarkson_Core \Objects ;
22
22
use WP_Post ;
23
+ use WP_Query ;
23
24
24
25
class FourOFour {
25
26
@@ -28,6 +29,7 @@ protected function __construct() {
28
29
add_filter ( 'clarkson_core_template_context ' , array ( $ this , 'add_404_object ' ), 11 );
29
30
add_action ( 'the_posts ' , array ( $ this , 'set_404 ' ), 10 , 2 );
30
31
add_action ( 'wp ' , array ( $ this , 'force_404 ' ) );
32
+ add_action ( 'pre_get_posts ' , array ( $ this , 'exclude_404_from_search ' ) );
31
33
}
32
34
33
35
public function set_404 ( $ posts , $ query ) {
@@ -137,6 +139,26 @@ public function field_content( $post_states, $post ) {
137
139
return $ post_states ;
138
140
}
139
141
142
+ /**
143
+ * Exclude selected 404 page from search results
144
+ */
145
+ public function exclude_404_from_search ( WP_Query $ query ): void {
146
+ $ excluded = apply_filters ( 'clarkson_404_exclude_from_search ' , true );
147
+
148
+ if ( ! is_admin () && is_search () && $ query ->is_main_query () && $ excluded ) {
149
+ $ id = get_option ( 'clarkson-page-for-404 ' , false );
150
+
151
+ if ( empty ( $ id ) ) {
152
+ return ;
153
+ }
154
+
155
+ $ excluded_posts = $ query ->get ( 'post__not_in ' ) ?: array ();
156
+ array_push ( $ excluded_posts , $ id );
157
+
158
+ $ query ->set ( 'post__not_in ' , $ excluded_posts );
159
+ }
160
+ }
161
+
140
162
protected $ instance = null ;
141
163
142
164
public static function get_instance () {
0 commit comments