I know how to use include and exclude parameters of the get_posts function but how do I tell get_posts to exclude a document and every child page under it ?
get_posts won't search for child posts if you exclude something, you have to tell it exactly what you want excluded
I know that I could collect all the IDs of the child pages using get children() and then append an array of these in the exclude parameter, but is there not a better and faster way ?
That's almost how you should do it, except:
- don't use
WP_Query for both
- don't use the exclude parameter, ask for more posts than you need then check for it manually in PHP, this will be significantly faster as exclusion makes querys very expensive ( as the number of posts increases, so does the cost of these queries )
Remember, always query for what you want, not what you don't want. When you say you don't want something, the database has to collect every post, filter out those that don't match to create a second data set, then run the query without the exclusion on the new data set. Even if you only want 2 or 3 posts, it loads everything. This is true be it excluding post IDs, posts in a category, posts that have a particular post meta value, etc