Skip to content

Instantly share code, notes, and snippets.

@kjtolsma
Last active March 3, 2021 09:45
Show Gist options
  • Save kjtolsma/81eb74ed217ece239fb1dfdbf09d6cce to your computer and use it in GitHub Desktop.
Save kjtolsma/81eb74ed217ece239fb1dfdbf09d6cce to your computer and use it in GitHub Desktop.
/**
* Handle 404's
*/
function prefix_event_posts( $result = false, $wp_query ) {
if ( is_admin() ) {
return false;
}
if ( 'your_post_type_name' !== $wp_query->query['post_type'] ) {
return false;
}
$post_id = 20 // Post to load
/* Load post */
$post_to_load = get_post( $post_id );
/* Create new post */
$new_post = new WP_Post( (object) $post_to_load );
$wp_query->post = $new_post;
$wp_query->posts[] = $new_post;
$wp_query->post_count = 1;
return true;
}
add_filter( 'pre_handle_404', 'prefix_event_posts', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment