Use The Image In A Blog Post In Search Results

  • UPDATE: it's working, but because I hidden a variable in testing on the 'blog_search_result.php" page to this:

    If you remove the filtering, you probably won't get the expected result. If things are working fine up to the layout stage, the next step would be looking into whether the layout variable is holding the expected value. Try outputting it:

    1. perch_layout_var('url');

    Do you get the result URL as expected?

  • Hmm, no. It acts weird with that option on - showing half a link, and a messy layout of the posts. And it's still looping.

    Here's a screen of the looping output, showing many "posts" DIVs, when there should only be one.

    Here is the search-result.html code. I must have missed something inside here...

  • I meant to output it on its own like so:

    1. perch_layout_var('url');
    2. perch_blog_custom([
    3. 'template' => 'post_search_result',
    4. 'filter' => 'postURL',
    5. 'value' => perch_layout_var('url', true),
    6. ]);

    If you have debug mode turned on, you can output it to the debug message instead:

    1. PerchUtil::mark('Layout var');
    2. PerchUtil::debug(perch_layout_var('url', true));

    Ultimately, you need the variable to hold the correct postURL value.

  • That just changed the output to a raw list of URLs of the posts. I'm not looking for that - it needs to output the code from post_search_result.html

    It was using it correctly, but looping as I mentioned above. Any other ideas?

  • I realise it is not what you're looking for; it wasn't meant to be the solution. I'm hinting quite badly for you to test every step so you'd figure out what is not working. As things stand, it sounds like the filtering on perch_blog_custom() is not working as expected.

    If you do get a URL from perch_layout_var('url'), copy one and hardcode it:

    1. perch_blog_custom([
    2. 'template' => 'post_search_result',
    3. 'filter' => 'postURL',
    4. 'value' => '/newsroom/unicorn',
    5. ]);

    This is just to confirm that the filtering works.

  • Sure, when I add the layout line below in my blog_search_result.php

    1. perch_layout_var('url');

    I get a result of the URL as a raw list of results that makes sense to the search, such as:

    1. /newsroom/what-telecom-partners-talk-about
  • Does either of the below return any results? p.s. you can try these anywhere on the page; it does not have to be inside the layout.

    1. perch_blog_custom([
    2. 'filter' => 'postURL',
    3. 'value' => '/newsroom/what-telecom-partners-talk-about',
    4. ]);
    5. perch_blog_custom([
    6. 'filter' => 'postSlug',
    7. 'value' => 'what-telecom-partners-talk-about',
    8. ]);
  • AH, interesting! It's now results look correct, but it's only showing ONE singles result. Even though with different searches, the results number varies and seems accurate to the search. I added the "count" option, but no luck. Suggestion?

  • Sorry for my reply delay. It's showing just one result for a search that has many results. Even though the first result is correct. Am I missing count or another option?

    1. <?php
    2. perch_content_search(perch_get('q'), [
    3. 'apps' => ['PerchBlog'],
    4. 'count'=>1000,
    5. 'template'=>'search-result.html',
    6. 'from-path' => '/newsroom',
    7. ]);
    8. ?>
  • Hi,

    I've managed to successfully implement this, however I can't seem to change the order of the search results. I'd like the latest to show first, but it shows the oldest first. I've tried adding the 'sort-order'=>'DESC' however this doesn't seem to work.

    Any help much appreciated.


  • I've just come across another issue. When there are multiple pages of results (I currently have the count at 9), no results show on the other pages, however it is saying there are 3 pages. Here is my code.

  • I've just come across another issue. When there are multiple pages of results (I currently have the count at 9), no results show on the other pages, however it is saying there are 3 pages.

    If you use conditional tags to filter the results inside the template like this, you cannot reliably using pagination. This is the expected behaviour. I noted this in a blog post.

    • Does your search form only search blog posts, or does it search other content on the website too?
    • Do you use the default blog post fields (as shipped with the Blog app), or do you use custom fields?
  • I just want to note that the initial solution on this thread is meant for a search form that searches all your site's contents, not just the blog:

    1. <perch:if id="result_source" value="PerchBlog">
    2. <!--* Search result from the Blog app *-->
    3. <perch:layout path="blog_search_result" url="<perch:search id="result_url">">
    4. <perch:else>
    5. <!--* Everything else *-->
    6. <!--* you are meant to display other results here and not leave it empty *-->
    7. </perch:if>

    If you only need to search the blog posts, it is probably easier to use the filtering options with perch_blog_custom():

    If you must use perch_content_search(), but still need to restrict the search to the Blog app (instead of performing the filtering inside the template):

    1. perch_content_search(perch_get('q'), [
    2. 'apps' => ['PerchBlog'],
    3. ]);