I have created a custom search that will only display results from a specified category. If you would like to do the same follow steps 1 & 2.
- Create a form for a search:
Save it as advanced.php in the root of your theme / child theme. You will already have a search.php file in this directory.
/blog/ is the route to your blog posts - if you receive no results check this.
The value of cat_slug becomes your category in which it will search.
Add to functions.php:
function advanced_search_query( $query ) { // check if search AND if "cat_slug" input was present if( $query->is_search() && ! empty( $_GET['cat_slug'] ) ) { // find the category by the slug passed in the input $term = get_category_by_slug( $_GET['cat_slug'] ); // defensive check, in case the category could not be found if ( ! empty( $term->term_id ) ) { // get the category ID $cat_id = $term->term_id; // set the query argument to search within the category $query->set( 'cat', $cat_id ); } } } add_action('pre_get_posts', 'advanced_search_query');
Now the search results are narrowed down. I need to adjust which elements of the post I can see.
At this point I have created my blogs relevant to this search with a custom template utilising ACF as you can see in my form the category has the slug 'case-study'. We don't require all the fields I have used. The fields relevant to the search are:
repeater - case_study_page_content
sub_fields - title
sub_fields - author
sub_fields - content
I have began editing my content.php file contained within the template-parts directory to display the results, this is looking like this:
class="blog-post">
ID), 'thumbnail' ); ?>
This is where I have ran into my complication as I am unable to just call the_field('author'); into the results loop.
Has anyone else been able to successfully pull values from within ACF's used in a custom blog page template into the search results page?