最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Search pages that are a child of the current page

programmeradmin1浏览0评论

I'm developing my own Wordpress theme and I was wondering if it is possible to modify the search query in a way that it only return results that are child pages of the actual page.

The reason I want this is that our site is getting really big and the user should be able to navigate to a parent page and then search for pages in this topic.

I give you an example. Our site is about the chemistry studies and has a page tree of the following form:

  • Home
  • Tipps & Ticks
  • Practical courses
    • Organic Chemistry
      • Synthesis
        • ...
      • ...
    • Inorganic Chemistry
      • Proof for anions
        • Sulfide
        • ...
      • ...
    • Physical Chemistry

So if the visitor has navigated to the "practical courses" page, the search form should only search for the input on it's child pages. Same for the subpages of that page. This would simplify the process of finding the right article enormously as the user can add "filters" by navigating to a specific page.

I'm happy for all answers and ideas. Regards, Chwebo

I'm developing my own Wordpress theme and I was wondering if it is possible to modify the search query in a way that it only return results that are child pages of the actual page.

The reason I want this is that our site is getting really big and the user should be able to navigate to a parent page and then search for pages in this topic.

I give you an example. Our site is about the chemistry studies and has a page tree of the following form:

  • Home
  • Tipps & Ticks
  • Practical courses
    • Organic Chemistry
      • Synthesis
        • ...
      • ...
    • Inorganic Chemistry
      • Proof for anions
        • Sulfide
        • ...
      • ...
    • Physical Chemistry

So if the visitor has navigated to the "practical courses" page, the search form should only search for the input on it's child pages. Same for the subpages of that page. This would simplify the process of finding the right article enormously as the user can add "filters" by navigating to a specific page.

I'm happy for all answers and ideas. Regards, Chwebo

Share Improve this question asked May 9, 2016 at 17:59 SamSam 4356 silver badges18 bronze badges 4
  • By the way, a related recent question is here, which you may find useful. It is about categories though so the answer is going to be different. – Tim Malone Commented May 9, 2016 at 22:57
  • How did you go with this Chwebo? – Tim Malone Commented Jul 6, 2016 at 4:06
  • I'm currently setting up some custom post types with custom taxonomies to achieve my goals without being limited to the first level. I think that's the best idea because the search will be faster and more effective and as a nice side-effect, the backend structure is more clear. – Sam Commented Jul 7, 2016 at 9:23
  • That makes sense, and sounds like a better solution. This answer is currently marked "unanswered" in our questions list, so feel free to add an answer with your solution and accept it if you like. – Tim Malone Commented Jul 7, 2016 at 9:29
Add a comment  | 

1 Answer 1

Reset to default 1

I started this in a comment but thought I'd expand it for you because I do like the question ;)

I'd probably tackle this by adding a hidden field to the search form with the current page ID, and then using the pre_get_posts filter to modify the search query to only return pages with a matching parent.

However: I don't think we can deal with an additional level of subpages in this way, just the first level. I know this answer therefore doesn't completely answer your question, but it's a start, and I'll add to it if I can figure this out (in the meantime, hopefully someone else can use this as a base for their own answer).

1. Adding the hidden field

In your theme, you'll need to create a searchform.php if it's not already there, in order to modify the default form that is output. If you're creating this from scratch, you can grab the existing form from this page.

We'll then add a hidden field, in between the <form></form> tags, printing out either the ID of the current page, or its parent page if we're not at the top level:

<?php if(is_page()){ ?>
  <input type="hidden" name="current_page_id" value="<?php echo $post->post_parent == 0 ? $post->ID : $post->post_parent; ?>" />
<?php } ?>

2. Hooking into pre_get_posts

In your theme's functions.php, we'll need to hook into Wordpress' pre_get_posts action to modify the query to exclude pages that aren't in our desired hierarchy. Of course, we want to make sure we only do this when doing a search:

add_action("pre_get_posts", "wpse_226092_search_by_page_parent");

function wpse_226092_search_by_page_parent($query){
  if(!is_admin() && $query->is_main_query() && is_search() && isset($_GET["current_page_id"])){
    $query->set("post_parent", $_GET["current_page_id"]);
  }
}

There's a few other conditionals in that code there, to make sure we don't affect the admin panel or other queries on the page (such as menu queries), and of course to make sure our custom value is actually set before we try to use it.

I haven't tested this, but fairly sure it should work!

发布评论

评论列表(0)

  1. 暂无评论