I've got several search forms on my site and I want them to show different results. My website has a very strict hierarchy and the search form on a parent site should only show results from it's child pages.
My plan was to include different hidden fields on the different parent pages which contain the id of that particular page. In the search.php
I then wanted to process the results and filter out the pages and posts that have no relation to the parent page.
Is there an easy way on how to achieve this?
Thanks in advance.
EDIT 1
This is my
search.php
<?php
if (have_posts()){
while(have_posts()){
the_post(); ?>
<div>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<p><?php echo get_the_author(); ?> - <?php echo get_the_date(); ?></p>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php
}
} else{ ?>
<h3>Sorry</h3>
<p>We are sorry but we could not find any matching articles on our site. Please try again with an other search request.</p>
<?php
get_search_form ();
}
?>
And the searchform.php
:
<form role="search" method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<label>Search...</label>
<input type="text" name="s" id="s" value="<?php echo get_search_query(); ?>" placeholder="Search..." />
<input type="hidden" name="post_parent" value="<?php echo (int)get_the_ID(); ?>" />
<button type="submit"><i class="fa fa-search" aria-hidden="true"></i></button>
</form>
EDIT 2
I also added <?php wp_reset_query(); ?>
before the if(have_posts()){}
. This results in no change. The pages are still shown.
wp_reset_query
to clean up afterquery_posts
calls, I recommend avoiding both