You can probably do something like this. It’s probably less efficient, but should find all descendants of a given page rather than just direct children.
function find_descendants($post_id) {
$descendant_ids = array();
$pages = get_pages("child_of=$post_id");
foreach ($pages as $page) { array_push($descendant_ids, $page->ID); }
return $descendant_ids;
}
function SearchFilter($query) {
if ($query->is_search) {
$query->set ( 'post__in', find_descendants(21) );
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
Related Posts:
- How to limit search to first letter of title?
- Using dynamic conditions in ‘posts_where’ filter
- Custom search filter causes menu and query_posts problems
- Filter the query ONLY for the search results page
- Problem with ‘post__not_in’
- Sort X categories by last update and show image
- using post__in allow duplicate post id
- Counter code for paginated category pages in wordpress
- pagination in author.php template returns 404 error
- How to modify the query to exclude posts by slug?
- Custom search page and search by title, content and tag
- Taxonomy.php issue with search and filters
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- Search stores (custom postcode field) within 5, 10, 15 miles of user inputted postcode.. Code modification
- Querying posts with meta value that begins with a certain pattern
- Adding Variables to post query
- Tricky WP Query
- query_posts() ALWAYS displays something?
- Error sending array of inputs
- Search breaks when querying main loop for category
- Using ajax with paging and a custom sub-query
- define orderby URL with meta_key=post_views_count
- How to remove a plugin filter’s priority on specific loops (custom queries)?
- Each post is showing twice in my custom query…?
- query_posts() – problem with “s” parameter
- Filter post query to only show direct children of category
- Exclude posts by post meta value
- Currently using a query_posts() in my theme – want to use a request filter in stead
- Custom search SQL Query to add custom field in result
- Can I use WP_Query one time and then filter the results multiple times
- Different layout based on post amount?
- How to solve this without flushing the rewrite rules for each post query the visitor triggers?
- How to check a list or feed of all posts under a category and tag?
- Best Query for blog posts
- Pre-existing arguments when using queries
- How can I modify this code to make the search box include tags and meta
- Customising the default wordpress search functionality
- WP Query Relations / Compare
- query_posts() with multiple meta data comparisons
- Adding Category Argument into an Array for WordPress Query
- Filter posts by multiple checkbox categories
- Is there a way to exclude the content from the post variable to save on RAM usage?
- Display/query post formats
- query_posts() in function makes global $wp_query out of sync?
- WordPress Custom Query
- Search with filters and title
- How to order posts by modified date without using ‘query_posts’?
- Help altering a query to exclude all but standard post format
- Post Title displaying but not in the wrapped HTML I need
- get_post_meta causes database queries
- Custom Queries – Child Categories not showing up
- Why is that gdsr_sort=thumbs (GD start rating plugin) only works with query_posts and not with WP_Query?
- Query_posts $query_string
- How to order posts by slug using query_posts/ Wp_query
- Order by meta_key with two meta_queries
- What is the difference between “manage_{$post_type}_posts_columns” and “manage_edit-{$post_type}_columns”?
- How to show only today’s post?
- use query_posts to return a post OR a page by ID
- query_posts for child pages
- orderby:date not working
- Custom filter in admin edit custom post type responding with invalid post type?
- Single meta key with multiple meta value in like comparison
- creat filter with wp_query
- How to filter my search in post if contains a word in title, content or excerpt?
- Filter the_posts doesn’t work on search page
- Posts and Attachments with “Published” status in Search
- Sorting a query Field by date
- Trying to package posts for republishing by partner offsite
- number of posts per page + random + exclude categories
- How to Query Post
- query post limits
- Get a list of posts by specific category
- query_post and wpdb returning different results
- Exclude current sticky post
- How to query two meta fields and display results between them
- WordPress Custom Query to get Most Commented Posts in the Past 7 Days
- Should I reset $wp_query?
- Ideas how to search & replace post_content when string contains a newline?
- Query posts only with actual text content (not including shortcode or images)
- How to manage a particular “order by” for get_search_query()?
- WP MS: How to query over the network
- Restrict query_posts by Date?
- Search a title word through query_posts (not the exact match)
- Notting but the latest post content keeps on being loaded
- query_posts doesnt show pages with given ID when post_type => page
- Custom loop off by one post
- How to setup blog page to render blog posts minus afew categories
- Trying to get all links in my posts
- Creating a custom WP query for a Custom Post Type
- Ordering of posts, 2020 Year showing as next event when should be last
- Modify WordPress search behaviour in backend?
- Re-order search results with posts_orderby filter and post meta value
- How to sort posts according to meta value?
- How to display most popular post by views count in WordPress?
- How to grab data (titles, thumbnails and custom fields) from multiple posts to populate a new array efficiently?
- Query posts in a category and include only one post per author?
- Form Data Causes “Page Not Found”
- Does wp_query and query_posts affect website performance? [duplicate]
- Custom Filtering date with newsletter
- Search and Filter Using Custom Post Type, Custom Taxonomy, and Advanced Custom Fields