Search for Blog not building proper URL for result

  • Hello,


    Revisiting this problem from a year ago. I have built a search page from the blog section of a site and the search is functioning except for one thing: the URL for for the found pages doesn't include the blog slug. For example, the result_url for the found page will be "//2019-04-22-new-adoption" rather than "/blog/2019-04-22-new-adoption".


    The search results page uses this code:


    Debug shows this:

    Quote
    get_route(): ">[52] SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate, pr.routeOrder, s.settingValue AS siteOffline FROM perch3_pages p LEFT JOIN perch3_page_routes pr ON p.pageID=pr.pageID LEFT JOIN perch3_settings s ON s.settingID='siteOffline' UNION SELECT NULL AS pagePath, pr2.routePattern, pr2.routeRegExp, pr2.templatePath AS pageTemplate, pr2.routeOrder, NULL AS siteOffline FROM perch3_page_routes pr2 WHERE templateID!=0 ORDER BY routeOrder ASC, pagePath ASC
    get_route(): ">Matched page: /search_results, so not using routes.
    Using master page: /templates/pages/search_display.php
    get(): ">[36] SELECT DISTINCT settingID, settingValue FROM perch3_settings WHERE userID=0
    find_by_path(): ">[1] SELECT * FROM perch3_pages WHERE pagePath='/search_results' LIMIT 1
    template_attributes(): ">Using template: /templates/pages/attributes/default.html
    find_all_tag_ids(): ">Using sub-template: /templates/pages/attributes/seo.html
    find_all_tag_ids(): ">Using sub-template: /templates/pages/attributes/social.html
    _get_content(): ">[1] SELECT regionKey, regionHTML FROM perch3_content_regions WHERE regionPage='/search_results' OR regionPage='*' ORDER BY regionPage DESC
    __construct(): ">Using template: /templates/search/search-form.html
    search_content(): ">Search term: programs for adoption and surrogacy
    search_content(): ">[10] SELECT SQL_CALC_FOUND_ROWS DISTINCT 'PerchContent_SearchHandler' AS source, MATCH(ci.itemSearch) AGAINST('programs for adoption and surrogacy') AS score, r.regionPage AS col1, ci.itemSearch AS col2, ci.itemJSON AS col3, r.regionOptions AS col4, p.pageNavText AS col5, p.pageTitle AS col6, regionTemplate AS col7, r.regionKey AS col8 FROM perch3_content_regions r, perch3_content_items ci, perch3_pages p WHERE r.regionID=ci.regionID AND r.regionRev=ci.itemRev AND r.pageID=p.pageID AND r.regionPage!='*' AND r.regionSearchable=1 AND (MATCH(ci.itemSearch) AGAINST('programs for adoption and surrogacy') OR MATCH(ci.itemSearch) AGAINST('programs for adoption and surrogacy') ) AND (r.regionPage LIKE '/%') UNION SELECT 'PerchContent_RunwaySearch' AS source, MATCH(ci.itemSearch) AGAINST('programs for adoption and surrogacy') AS score, c.collectionKey AS col1, ci.itemSearch AS col2, ci.itemJSON AS col3, c.collectionOptions AS col4, c.collectionKey AS col5, c.collectionKey AS col6, collectionTemplate AS col7, c.collectionKey AS col8 FROM perch3_collections c, perch3_collection_items ci, perch3_collection_revisions cr WHERE c.collectionID=cr.collectionID AND cr.itemID=ci.itemID AND cr.itemRev=ci.itemRev AND c.collectionSearchable=1 AND cr.itemSearchable=1 AND (MATCH(ci.itemSearch) AGAINST('programs for adoption and surrogacy') OR MATCH(ci.itemSearch) AGAINST('programs for adoption and surrogacy') ) UNION SELECT 'PerchBlog_SearchHandler' AS source, MATCH(postTitle, postDescRaw, postTags) AGAINST('programs for adoption and surrogacy') AS score, postTitle, postSlug, postDateTime, postDescHTML, postID, sectionSlug, "", "" FROM perch3_blog_posts p, perch3_blog_sections s WHERE postStatus='Published' AND postDateTime<'2019-05-13 16:17:54' AND p.sectionID=s.sectionID AND MATCH(postTitle, postDescRaw, postTags) AGAINST('programs for adoption and surrogacy') ORDER BY score DESC LIMIT 0, 10
    get_value(): ">[59] SELECT FOUND_ROWS() AS `count`
    search_content(): ">[10] Using template: /templates/search/search-result.html
    Request time: 0.0429
    Process time: 0.0428
    Memory: 1.3696

    And showall gives me this:


    Quote
    perch_page_path /search_results
    url //2019-04-22-new-adoption-surrogacy-programs-in-ovia-health
    title New adoption & surrogacy programs in Ovia Health
    excerpt Ovia Health's maternity & family benefits now include programs for adoption and surrogacy{...}
    key programs for adoption and surrogacy
    source PerchBlog
    result_url //2019-04-22-new-adoption-surrogacy-programs-in-ovia-health
    result_title New adoption & surrogacy programs in Ovia Health
    result_excerpt Ovia Health's maternity & family benefits now include programs for adoption and surrogacy{...}
    result_key programs for adoption and surrogacy
    result_source PerchBlog
    search_key programs for adoption and surrogacy
    paging true
    total 59
    number_of_pages 6
    total_pages 6
    per_page 10
    current_page 1
    lower_bound 1
    upper_bound 10
    prev_url
    next_url /search_results?q=programs+for+adoption+and+surrogacy&page=2
    prev_page_number
    next_page_number 2
    first_page_url /search_results?q=programs+for+adoption+and+surrogacy
    last_page_url /search_results?q=programs+for+adoption+and+surrogacy
    first_page true
    not_last_page true
    perch_item_first true
    perch_item_zero_index 0
    perch_item_index 1
    perch_item_rev_index 10
    perch_item_rev_zero_index 9
    perch_item_odd
    perch_item_count 10
    perch_index_in_set 1
    perch_zero_index_in_set 0
    perch_first_in_set true
    perch_namespace perch:search

    Finally, in settings, theBlog post page path is

    Code
    1. /{blogSlug}/{postSlug}

    and the slug format is:

    Code
    1. %Y-%m-%d-{postTitle}

    I'm at a complete loss.


    Thanks, Tim

  • drewm

    Approved the thread.
  • I discovered that this:

    PHP
    1. <?php
    2. $query = perch_get('q');
    3. perch_content_search($query, array(
    4. 'count'=>10,
    5. 'excerpt-chars'=>300,
    6. 'apps'=>'PerchBlog',
    7. 'no-conflict' => true,
    8. 'template'=>'search-result.html'
    9. ));
    10. ?>

    Specifically the 'apps'=>'PerchBlog' was what was causing the problem. Changing it to 'result_source'=>'PerchBlog' fixed the problem.

    PHP
    1. <?php
    2. $query = perch_get('q');
    3. perch_content_search($query, array(
    4. 'count'=>10,
    5. 'excerpt-chars'=>300,
    6. 'result_source'=>'PerchBlog',
    7. 'no-conflict' => true,
    8. 'template'=>'search-result.html'
    9. ));
    10. ?>
  • Hi,


    The value of the apps option needs to be an array not a string:



    To restrict search to certain apps, pass an array of app names (e.g. PerchBlog, PerchEvents), otherwise search is unrestricted.


    So to restrict the search to the Blog app you would use:


    PHP
    1. perch_content_search($query, array(
    2. 'count' => 10,
    3. 'excerpt-chars' => 300,
    4. 'apps' => ['PerchBlog'],
    5. 'no-conflict' => true,
    6. 'template' => 'search-result.html'
    7. ));
  • Well, I thought I'd resolved it, but that was premature. Tried the above and that didn't work.


    My blog settings are:

    Code
    1. perch_blog_post_url: /{blogSlug}/{postSlug}
    2. perch_blog_slug_format: %Y-%m-%d-{postTitle}

    With perch:showall, the URL of the blog post that displays in search results is missing the full address, which is present in the blog post itself. So, for a post with a URL of:


    http://testserver.com/blog/2019-04-22-new-adoption


    That same URL in the search results is:


    //2019-04-22-new-adoption


    Anyone?

  • Still haven't solved this problem. It appears that for some reason the search results page is not getting the postURL for the URL, but I haven't been able to figure out how to rectify it.

  • It looks like the Blog app search handler does not return the blog slug.


    So a workaround would be to check whether the result source is PerchBlog, grab the post slug from the incomplete result URL and then get the correct post URL using the slug.


    You can't use the each option with perch_content_search() as far as I'm aware. The other possible approaches I can think of here are using a template filter or a layout include.



    With a layout include, your template would be something like this:


    HTML
    1. <a href="<perch:layout path="search_result_url" url="<perch:search id="result_url">" source="<perch:search id="source">">" >
    2. <perch:search id="result_title">
    3. </a>


    And your /perch/layouts/search_result_url.php would be something like:




    If you are comfortable writing a template filter, your template would be easier to read:


    HTML
    1. <a href="<perch:search id="result_url" filter="searchurl">" >
    2. <perch:search id="result_title">
    3. </a>