Support » Plugin: WP Search with Algolia » Display custom indexed attribute in Frontend

  • Resolved reti97

    (@reti97)


    Hello altogether

    I’m building a custom search results page. To improve this, I indexed custom attributes at algolia. These are sent to the index with functions in the functions.php file. Since I need them for each post / page, they are also indexed at $shared_attributes.
    So for example

    function featured_image( array $shared_attributes, WP_Post $post ) {
            if (!empty(get_the_post_thumbnail_url( $post->ID, 'full' ))) {
                 $shared_attributes[ 'fullsize_img' ] = get_the_post_thumbnail_url( $post->ID, 'full' );
            }
    
            return $shared_attributes;
        }
    
    add_filter( 'algolia_post_shared_attributes', 'featured_image', 10, 2 );

    If I login to algolia.com and take a look at the index, I can see that the attribute (fullsize_img) has been indexed.

    Next step (and that’s where my problem comes in):
    I want to display the attribute in my instantsearch.php, so on my search results page. I thought I can just get it with {{attribute-name}} (so the example would be: {{ fullsize_img }} ) like any default attribute. This doesn’t seem to work.
    Did I forget something?
    Do I have to set something up to be able to display the value on the front end?

    Thank you in advance and have a nice day
    Reti

    • This topic was modified 2 months ago by reti97.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’m curious if it’s still a property on the populated javascript variables coming in, but perhaps in a different spot than where you’ve been looking at thus far.

    Are you on the latest version of the released plugin?

    Thread Starter reti97

    (@reti97)

    Hey Michael
    Thank you for your prompt reply. I believe your term for the populated javascript variables coming in is a good place to start. Where are these variables defined or rather, how do I define which variables I want to use? (Did I get that right, that these are the ones I can display in instantsearch.php?)

    The plugin should be on the latest Version (2.1.0).

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    I don’t know if we have any list typed out anywhere, but looking at the instantsearch.php file and the <script> sections would show the ones we have used in the template files. If you set a breakpoint in your Sources/Debugger sections of your browser dev tools, you could get a pretty good idea of javascript state at the point of execution.

    Thread Starter reti97

    (@reti97)

    Alright, thank you, that helped me, I now found where the problem is. I think it would be quite easy to solve if I’d know how.
    I’ve seen in the script tag, that the returned results are from the “searchable posts” index.
    My custom attributes are just indexed in the “posts_post” and the “posts_page” index. Am I doing something wrong (wrong variable or something) in the function (the one from the first post)?

    Thread Starter reti97

    (@reti97)

    Alright, just found out that I can replace add_filter( 'algolia_post_shared_attributes', 'featured_image', 10, 2 ); with add_filter( 'algolia_searchable_post_shared_attributes', 'alg_excerpt', 10, 2 ); and it gets indexed in the searchable posts index.
    I’ve just checked, I’m now able to display the content in the frontend. So I’m able to display what I wanted.
    Thanks for your time and your help and have a nice day
    Reti

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’d need to dig in some more to see if there’s a different filter that would allow appending custom content to other spots, as I’m not sure off the top of my head.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Looks like I forgot to refresh before I replied. Glad to hear you found something that’s working for you 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.