• 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 6 years, 6 months ago by sebgc.
    • This topic was modified 6 years, 6 months ago by sebgc.
    • This topic was modified 6 years, 6 months ago by sebgc.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor 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.

    Thread Starter sebgc

    (@sebgc)

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

    Thread Starter sebgc

    (@sebgc)

    Any suggestions for the fix?

    Thread Starter sebgc

    (@sebgc)

    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)

The topic ‘Adding custom fields to index’ is closed to new replies.