Display blog section in search results

  • Working on a multi-lingual build using perch runway and within the blog app each language is separated into sections i.e EN, DE, ES etc.


    Each language sets a global variable dependant on the url and I need to filter the search results from the source relative to the blog section along with pages and collections.

    It seems using the normal 'perch_content_search' outputs all sections from the source 'PerchBlog' and is also watered down compared to pages or collections.


    Any suggestions on how best to achieve the desired result as well along with results from pages and collections?

  • Each language sets a global variable dependant on the url and I need to filter the search results from the source relative to the blog section


    If I understand correctly, you'd like to display the results in the same language as the global variable you're setting. Is this correct? For example, if you are on /de/search, you want to only display results with URLs that start with /de?

  • From the screenshot it looks like you are using multiple Blogs, which is different from Blog Sections.


    Anyway, I don't think there is a straightforward way to only include posts from one blog (e.g. the DE blog) in the search results. There are workarounds though.


    One option is to get all the matching items, skip the templating, filter the items in PHP and output the filtered items with perch_template():


  • hus_hmd we do already utilise the result source within the results template.


    So the main issue is that the site uses the branched method. Each site has its own blog which uses the multiple blogs feature available in runway to manage. These have been named EN etc. As seen in the previous screenshot.


    The problem that arises in the search results is that all blog posts are seen the same, a post that features on the DE blog might crop up search results on the EN site search along with EN blog posts. The issue is that from the result url that is no way to know that a blog post is from the DE blog to filter out. The url will always appear /en/latest/german-blog-post/


    Thinking about it, I’m not sure if updating the blog post url in settings may help.


    Does that make sense?

  • If the post URL does not include the blog slug (which would correspond to the language in your case), you could use perch_blog_custom() to look up each post and include/exclude it from the search results accordingly.


    PHP
    1. $post = perch_blog_custom([
    2. 'skip-template' => true,
    3. 'filter' => 'postURL',
    4. 'value' => $item['result_url'],
    5. 'blog' => $lang,
    6. ]);


    This means you'd be making more database queries on the page though.