I found a solution in this answer here: https://stackoverflow.com/questions/63678063/wordpress-search-filtering-pages-and-posts
I customised the function as follows:
function page_search_filter( \WP_Query $query ) {
if ( ! is_admin() ) {
if ( $query->is_main_query() && $query->is_search() ) {
if ( isset( $_GET['post_type'] ) && $_GET['post_type'] == 'page' ) {
$query->set( 'post_type','page' );
}
}
}
return $query;
}
add_action( 'pre_get_posts', 'page_search_filter' );
Related Posts:
- Manual excerpts for pages not working on Search
- Custom Search Page Pagination Not Working
- When I split a large post with , how can a search locate users to the correct page?
- Form action redirect to the same page
- Two Search pages, One search form
- How get get list of pages in ajax search
- Getting to a specific page on website using specific code instead of navigation bar
- Search child pages from a specific page parent
- Add Filter to Ignore a Post/Page or a Number of Post/Pages
- Search results to be only posts & children of page ID
- Search anything from a Child and Sub-Pages of their Parent page
- Add pagination to search.php page
- Search Page: activate html code if the tag is on the page
- If searched in a category show different 404 page
- Search page different title per results
- search page different results
- simple category search form returns full text – but I need excerpts
- How to enable automatic search results in WordPress
- Save search criteria per user and show on custom user page
- Create a page without adding a page in the Database
- What methods to use to create small, editable pieces of text for static pages?
- Hide page visual editor if certain template is selected?
- What is an alternative to get_page_by_title()?
- Require title for pages
- Add a new page to wordpress programmatically
- Get Permalink for the top level parent of child pages
- Get page id by title?
- List all pages and children in separate ul
- Dynamically Load Styles and Scripts from Theme Functions.php
- How do I load custom scripts and styles for a page?
- How should I best incorporate WordPress in my static website?
- WP won’t save changes in post (any)
- Call the content of a page in AJAX in WordPress
- Created pages not showing up in ‘All Pages’ list
- Registering multiple sidebars with data from an array
- How to dynamically save a selected option from page “Templates” selectbox?
- Change names of pages in wordpress
- Extra title output with this function wp_list_pages
- Dynamically change page that loads
- Page title not showing up
- Where is the key _wp_page_template passed?
- Private Posts/Pages & Search
- remove/hide pages from users backend
- Related Links Page with moderated, user suggested link submissions
- Displaying page content in category archive (archive.php)
- Missing Page, But Still Exists In Preview
- How to create a page that lists all the experts(Users with same access permission as author)?
- Adding Author and Updated Schema Markup to WordPress Static Pages
- Trying to Edit a Page but it sends me to a Product Edit page
- My custom template page not loading content inside it?
- Can a user find a child page?
- How to edit a HTML list in WordPress?
- Is it possible to activate page template without manually selecting it in wp-admin?
- Excerpt Now Showing on the Post Page
- All Pages visible on the Frontpage
- How do we exclude multiple pages (like opposite of is_page)
- Create page template with some static contents
- Link to Particular Page Within Template PHP Code
- Can I schedule my site to launch at a set time and date?
- If I use a static page-slug.php for a page, is the database still queried?
- Group search results by category
- Using page-something.php as static front page
- Get all children titles of a page as a link to them
- Two different pages that share the same content?
- Having a series of pages created based on records in a table with fields populating the pages
- custom page templating in WordPress
- WordPress – Retrieve a Page if it’s a direct descendant of another Page
- After creating a new page, filling the page with structure
- Identify that a page is a grandchild page
- Suggestions: Customizable header image and style colors that are different for each page
- Page Slug Won’t Change – Can’t Edit Archive Page With Slug or Reassign Slug
- accessing the Admin page
- Cover block always has margin
- When I enter password, I’m redirected to the WordPress login form
- How do I add a new item to this menu on the Add New Page section?
- how to change permalink for dynamic page without 404 error
- Page and admin doesnt load – i tried everything
- Problem with deleting a “Page”
- Static Home Page resets to Front Page
- Display different pages based on form entry
- Adding styles/scripts to specific page
- Removing add new pages form editor role
- Maximum number of characters in a wordpress page?
- Hidden Pages, Posts & Plugins
- History of page, interval of years
- How to get sort content by page id?
- Multiple pages to show posts
- How to finish this loop?
- Page can not be updated and disappeared in “show all page”
- All the pages are blank [closed]
- Approve Page Edits
- CSS Styling for a Page/ Site made with Pagelines
- Static front page with excerpt
- How to set a default page for load first time?
- How do you add a featured image to a page (not post)?
- All pages duplicated like a mirror with critical errors
- Change the environment for creating a WordPress page
- display new WordPress page content
- Dashboard show only published pages instead of all pages
- May I know where to edit the tax rate?