Support » Plugin: WP Search with Algolia » Adding custom fields to index

  • Resolved sebgc

    (@sebgc)


    Hi

    What code is required to add additional custom fields as attributes of the index?
    I have tried using the filter hook to append new shared attributes but these values are not being indexed:

    function my_post_attributes( array $shared_attributes, WP_Post $post ) {
    	$shared_attributes['call_to_action_url'] = 'https://url.com';
    	return $shared_attributes;
    }
    add_filter( 'algolia_post_shared_attributes', 'my_post_attributes', 10, 2 );
    • This topic was modified 2 months ago by sebgc.
    • This topic was modified 2 months ago by sebgc.
    • This topic was modified 2 months ago by sebgc.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Where are you pasting your code above? What file or perhaps a custom plugin?

    Not seeing anything wrong with it as is, but I am kind of wondering if it’s being run after the filter has run, or if not in the right conditions somehow.

    Thanks for the assist. The code is pasted into my theme’s functions.php

    Any suggestions for the fix?

    There must have been an issue with my variable names – I have since corrected the implemented code and it works as expected, for example:

    
    /* Algolia customisations */
    add_filter( 'algolia_post_shared_attributes', 'complyadvantage_post_attributes', 10, 2 );
    add_filter( 'algolia_searchable_post_shared_attributes', 'complyadvantage_post_attributes', 10, 2 );
    
    /**
     * @param array   $shared_attributes
     * @param WP_Post $post
     *
     * @return array
     */
    function complyadvantage_post_attributes( array $shared_attributes, WP_Post $post ) {	
    	if ( 'resource' !== $post->post_type ) {
    		// We only want to add an attribute for the 'resource' post type.
    		// Here the post isn't a 'resource', so we return the attributes unaltered.
    		return $shared_attributes;
    	}
    	$shared_attributes['call_to_action_url'] = get_post_meta( $post->ID, 'wpcf-call-to-action-url', true );
    	// Always return the value we are filtering.
    	return $shared_attributes;
    }
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.