Skip to main content
The if statement did not have curly braces
Source Link
Jacob Peattie
  • 43.9k
  • 10
  • 49
  • 62

Although I'm also curious as to why you'd want to do this, and would probably suggest using a custom post type instead, this would probably work (actually works for any single post type except pages and attachments):

add_action( 'pre_get_posts', 'wpse44983_single_post_404' );
function wpse44983_single_post_404( $query ) {
    if ( $query->is_main_query() && $query->is_single() ) {
        $query->is_404 = true;
     }
}

Edit: added curly braces to the IF statement.

Although I'm also curious as to why you'd want to do this, and would probably suggest using a custom post type instead, this would probably work (actually works for any single post type except pages and attachments):

add_action( 'pre_get_posts', 'wpse44983_single_post_404' );
function wpse44983_single_post_404( $query ) {
    if ( $query->is_main_query() && $query->is_single() ) {
        $query->is_404 = true;
     }
}

Edit: added curly braces to the IF statement.

Although I'm also curious as to why you'd want to do this, and would probably suggest using a custom post type instead, this would probably work (actually works for any single post type except pages and attachments):

add_action( 'pre_get_posts', 'wpse44983_single_post_404' );
function wpse44983_single_post_404( $query ) {
    if ( $query->is_main_query() && $query->is_single() ) {
        $query->is_404 = true;
     }
}

Although I'm also curious as to why you'd want to do this, and would probably suggest using a custom post type instead, this would probably work (actually works for any single post type except pages and attachments):

add_action( 'pre_get_posts', 'wpse44983_single_post_404' );
function wpse44983_single_post_404( $query ) {
    if ( $query->is_main_query() && $query->is_single() ) {
        $query->is_404 = true;
     }
}

Edit: added curly braces to the IF statement.

Although I'm also curious as to why you'd want to do this, and would probably suggest using a custom post type instead, this would probably work (actually works for any single post type except pages and attachments):

add_action( 'pre_get_posts', 'wpse44983_single_post_404' );
function wpse44983_single_post_404( $query ) {
    if ( $query->is_main_query() && $query->is_single() )
        $query->is_404 = true;
}

Although I'm also curious as to why you'd want to do this, and would probably suggest using a custom post type instead, this would probably work (actually works for any single post type except pages and attachments):

add_action( 'pre_get_posts', 'wpse44983_single_post_404' );
function wpse44983_single_post_404( $query ) {
    if ( $query->is_main_query() && $query->is_single() ) {
        $query->is_404 = true;
     }
}

Edit: added curly braces to the IF statement.

Source Link
helenhousandi
  • 3.9k
  • 3
  • 22
  • 27

Although I'm also curious as to why you'd want to do this, and would probably suggest using a custom post type instead, this would probably work (actually works for any single post type except pages and attachments):

add_action( 'pre_get_posts', 'wpse44983_single_post_404' );
function wpse44983_single_post_404( $query ) {
    if ( $query->is_main_query() && $query->is_single() )
        $query->is_404 = true;
}