The most efficient way to do it would be to have a whitelist of pages, and to check the requested page against that list before the query is run.
function my_parse_query( $wp_query ) {
if( isset( $wp_query->query_vars['sub_pages'] ) ){
$sub_pages = array(
'example',
'another'
);
if( ! in_array( $wp_query->query_vars['sub_pages'], $sub_pages ) ){
$wp_query->set_404();
status_header( 404 );
}
}
}
add_action( 'parse_query', 'my_parse_query' );
Then, once you reach the post type template, you know that if sub_pages is set at that point, it contains one of your defined valid pages.
Related Posts:
- 404 error after publishing a post
- Some posts returning 404 instead of displaying post
- Custom post type single (permalink) throws a 404 error
- Unable to modify(update) posts – Page not found
- Debugging WP routing
- PHP Notice error (when on 404 page)
- Getting a 404 error when clicking edit page
- 404 on paginated blog pages
- How to display content from the main blog on another from multisite network as it would be its own
- Why does my site often display 404 on subpages?
- custom post template file not shown, instead all the time 404.php
- One post on my website is going to a 404 page
- How do I locate the exact location where this error is occurring 404 page not found
- 404 error on default post type and default taxonomy fronted page
- Problems with Page Not Found
- Posts and Pages: 404 Page not found
- If post does not exist, do something
- Prevent 404 of Author pages without posts
- 404 error on every post and page other than home
- Get post title of faulty link on 404 error page
- Getting 404 when using rewrite rule for posts
- Password-protected Posts lead to 404 Error
- Cannot Create a new Post [closed]
- Post pages are redirecting to the 404 page in wordpress
- Redirection – bulk edit 404s to new tomain
- removing inline styles from wp-caption div
- Different post sort order within different categories
- How to export posts with featured image
- How are terms connected with posts in database?
- JSON not valid after json_encode posts
- Post Admin – Filter by posts without tags
- Exclude category on blog list page
- Post & edit a post from front end along with upload, dropdown, and other inputs
- Text after more tag in posts
- Hide parts of the post content on the home page
- Create A Metabox For A Custom Field
- Get the post_id of a new post
- How to add custom content (text/image) at start of content (IN content ie the same line)
- Prevent post from being published if no category selected
- Load content in a div with ajax
- Get a custom field of all posts on current blog page
- Display posts in alphabetical order for a particular category
- Direct link to “New post” in specific category?
- Disable most recent & view all (TABS) on nav-menu.php
- Apply a style for 4 blogs and another style for the next 4 blogs in wordpress blog page?
- Show posts by a custom post author
- Add multiple tags to multiple posts
- Chrome + Ad Blocker hides the content of the post
- Posting blog entries in screenplay format
- Blank page when saving edits on post
- show custom post type category dropdown sorting result on same page
- How to exclude visited posts from loop
- Number of displayed posts
- Add custom text automatically on each post
- What date to use as a post date? date_gmt or modified_date_gmt
- Display current post position in Elementor Posts widget
- singular posts using archive styling
- My Custom Post Type Still Using index.php
- How to get the form code from the edit page of a specific post?
- Send email for pending post
- get_posts not pulling the correct number of posts
- change the post time on multiple posts
- If custom field is empty, use one from a previous post
- Making posts permalinks consistent numbers
- How to show category based post in home page?
- YARPP php question
- Classic Button in Classic editor
- Export post another site
- Css and function issue on author archive page
- Need to display a Jan 1st post as the site’s front page on Jan 1st, and Jan 2nd post as front page on Jan 2nd etc
- Related Customs posts
- How to add a ‘News’ section to specific posts in WordPress
- I have two post types in the same query, how can I use CSS to select only one post type?
- How to hide meta from search result only on pages?
- How do I display posts ordered by a date custom field?
- How to show category image if no featured image is set?
- How to add category base prefix to wordpress permalink
- Get post id outside loop : Notice: Trying to get property of non-object
- Display post on main page with thumbnail
- Redirecting user to a page when there is no content?
- How can I alternate styling of images in a post? [closed]
- Custom Single Post Type not referring to single-post-type.php File
- Display iframe or embeded posts on homepage
- index.php is only displaying current month’s posts?
- How can I use the “Your latest posts” as a template?
- Query to get data of a post, if in category?
- Order posts by custom field
- “Current” class on a singular page menu item with custom post types?
- Continuous listing from a custom field
- get next/previous post name
- Add class name of content, Hypernate doesn’t work to IE and Chrome
- How do I display the next 3 posts in the sidebar?
- 2 tick boxes appearing below comments
- Issue with wordpress pagination: last 2 posts show again on second page
- Extra Theme – Fit image inside of Featured Post Slider
- Polylang – display one post into 3 categories with different lang [closed]
- How can I show the positive and negative comments for a same post separately?
- Site ‘Categories’: save an admin global setting with post metadata [closed]
- I cannot see the pagination in /wp-admin posts page
- Insert Tag in WordPress Post