Forum Replies Created

Viewing 15 replies - 1 through 15 (of 28 total)
  • Thread Starter tonyp83

    (@tonyp83)

    Hi Michael, thanks for you feedback, as I thought it’d be a bit difficult to do in the current state as there’s no class or ID included in those divs.

    I’m going to just go with a show all option, as I suppose ultimately the goal should be to get the whole site translated eventually.

    Thanks,

    Thread Starter tonyp83

    (@tonyp83)

    Hi @tw2113 – I’ve tried deleting and re-uploading all files I’m updating (functions and the algolia files) and I can see it is now working, and just checking the Search on the german page it’s only going through the content on there.

    So all seems good now! Thanks for your support on this ??

    Thread Starter tonyp83

    (@tonyp83)

    Yeah, I can see when I view on Algolia two entries for my test page I have in English and German, both as seperate entries.

    Thread Starter tonyp83

    (@tonyp83)

    Hi Michael,

    I’ve received the translation and am started to flow that into the website now. My German content is searchable, but it’s also showing any results in English too, so the filtering of the locale doesn’t seem to have worked..

    Addition to my functions.php

    // Add the locale of every post to every record of every post type indexed.
    function add_locales_to_records( array $attrs, WP_Post $post ) {
    // Here we make sure we push the post's language data to Algolia.
    $attrs['wpml'] = apply_filters( 'wpml_post_language_details', null, $post->ID );
    return $attrs;
    }

    add_filter( 'algolia_post_shared_attributes', 'add_locales_to_records', 10, 2 );
    add_filter( 'algolia_searchable_post_shared_attributes', 'add_locales_to_records', 10, 2 );

    // Register the locale attribute as an Algolia facet which will allow us to filter on the current displayed locale.
    function add_locale_to_facets( array $settings ) {
    $settings['attributesForFaceting'][] = 'wpml.locale';

    return $settings;
    }

    add_filter( 'algolia_searchable_posts_index_settings', 'add_locale_to_facets' );
    add_filter( 'algolia_posts_index_settings', 'add_locale_to_facets' );

    // Expose the current locale of the displayed page in JavaScript.
    function enqueue_locale() {
    wp_add_inline_script( 'algolia-search', sprintf('var current_locale = "%s";', get_locale()), 'before' );
    }

    add_action( 'wp_enqueue_scripts', 'enqueue_locale', 99 );

    // Add the filtering
    function algolia_filter_backend_search_params( $params ) {
    $current_lang = apply_filters('wpml_current_language', NULL);

    if ( ! $current_lang ) {
    return $params;
    }

    $params['filters'][] = 'language:' . $current_lang;
    return $params;
    }
    add_filter( 'algolia_search_params', 'algolia_filter_backend_search_params' );

    Additions to instantsearch.php and autocomplete.php files,

      filters: 'wpml.locale:"' + current_locale + '"', // This is the added line.

    But even after pushing all indexes the search still finds content in English pages. I’m not sure if I’ve missed something or it’s me using Advanced Custom Fields that’s causing issues.

    Any ideas?

    Thanks,

    Thread Starter tonyp83

    (@tonyp83)

    Thanks Michael,

    Just disabling the button ID has done the job ??

    Thread Starter tonyp83

    (@tonyp83)

    I sadly don’t, as I only have WPML running on my localhost of the site, as the Staging site we have is being used to review and add pages and content, so I’ll only be adding WPML to Staging when that’s done the client’s end.

    I do have Pages, Posts and two Custom Post Types showing in the autocomplete menu. You can see what I describe here.

    I can see it’s registering my German pages when I look via Algolia’s Dashboard, so it all looks fine that side (screenshot of what I can see in my Dashboard).

    Just to check, here are my updates to instantsearch.php

                    /* Stats widget */
                    instantsearch.widgets.stats({
                        container: '#algolia-stats'
                    }),
    
                    instantsearch.widgets.configure({
                        facetingAfterDistinct: true,
                        highlightPreTag: '__ais-highlight__',
                        highlightPostTag: '__/ais-highlight__',
                        filters: 'wpml.locale:' + current_locale // This is the line we added.
                    }),

    My autocomplete.php

                source: algoliaHitsSource( client.initIndex( config[ 'index_name' ] ), {
                    hitsPerPage: config['max_suggestions'],
                    attributesToSnippet: [
                        'content:10'
                    ],
    
                    highlightPreTag: '__ais-highlight__',
                    highlightPostTag: '__/ais-highlight__',
                    filters: 'wpml.locale:"' + current_locale + '"', // This is the added line.
                }),

    And functions.php

    /**
    * @param WPML Algolia function
    * ref: https://github.com/WebDevStudios/wp-search-with-algolia/wiki/WPML
    *
    * @return array
    */

    // Add the locale of every post to every record of every post type indexed.
    function add_locales_to_records( array $attrs, WP_Post $post ) {
    // Here we make sure we push the post's language data to Algolia.
    $attrs['wpml'] = apply_filters( 'wpml_post_language_details', null, $post->ID );
    return $attrs;
    }

    add_filter( 'algolia_post_shared_attributes', 'add_locales_to_records', 10, 2 );
    add_filter( 'algolia_searchable_post_shared_attributes', 'add_locales_to_records', 10, 2 );

    // Register the locale attribute as an Algolia facet which will allow us to filter on the current displayed locale.
    function add_locale_to_facets( array $settings ) {
    $settings['attributesForFaceting'][] = 'wpml.locale';

    return $settings;
    }

    add_filter( 'algolia_searchable_posts_index_settings', 'add_locale_to_facets' );
    add_filter( 'algolia_posts_index_settings', 'add_locale_to_facets' );

    // Expose the current locale of the displayed page in JavaScript.
    function enqueue_locale() {
    wp_add_inline_script( 'algolia-search', sprintf('var current_locale = "%s";', get_locale()), 'before' );
    }

    add_action( 'wp_enqueue_scripts', 'enqueue_locale', 99 );

    I was wondering if within my functions file I needed to update any references..

    Thread Starter tonyp83

    (@tonyp83)

    Thanks Michael, I can see now where I need to follow.

    I’ve added those two lines to my versions of autocomplete.php and instantsearch.php (with the line from WP Search with Algolia version 2.0.x and later) as well as updating my functions.php file.

    I’ve re-indexed content and pushed those changes, and for the English versions everything is working as fine – but for the German pages I have the Autocomplete is picking up the translated page and any English pages and posts too with similar words, so it’s not just looking at the /de/ locale, but both.

    Just to check, if I’d implemented this correctly it would show only German pages when I was searching?

    Thanks,
    Tony

    Thread Starter tonyp83

    (@tonyp83)

    I’ve noticed a specific field where a repeater has a repeater field inside of it seems to break the indexing process, so I’m trying to amend that.

    Thread Starter tonyp83

    (@tonyp83)

    @tw2113 so I felt like I made some progress and have got in some instances Algolia to index content with my the_sub_field with my local version – but when I try and use on production (which has 51 pages already created) it deletes everything in the index so for wp_posts_page it shows 0 records. When I remove my function below and re-index it goes back to 51 pages, but no custom fields at all.

    So something seems to be breaking the index process it seems.

    function my_post_attributes_with_sub_field( array $attributes, WP_Post $post ) {
    
    	$fields = [
    		'hero_image',
    		'content_page_builder',
    	];
    
    	$ourdata = [];
    	foreach ( $fields as $field ) {
    		$datas = get_field( $field, $post->ID );
    		if ( ! empty( $datas ) ) {
    			foreach ($datas as $data) {
    				if ( ! empty( $data ) ) {
    					$ourdata[] = $data;
    				}
    			}
    		}
    	}
    
    	$attributes['acf_fields'] = $ourdata;
    
    	return $attributes;
    }
    
    add_filter( 'algolia_post_shared_attributes', 'my_post_attributes_with_sub_field', 10, 2 );
    add_filter( 'algolia_searchable_post_shared_attributes', 'my_post_attributes_with_sub_field', 10, 2 );
    
    
    /**
     * @param array $settings
     *
     * @return array
     */
    
    function my_posts_index_settings( array $settings ) {
    
    	$settings['searchableAttributes'][] = 'unordered(acf_fields)';
    
    	return $settings;
    }
    
    add_filter( 'algolia_posts_index_settings', 'my_posts_index_settings' );
    
    Thread Starter tonyp83

    (@tonyp83)

    Follow-up

    This issue is related to another, so will close.

    Thread Starter tonyp83

    (@tonyp83)

    I’ve tweaked this slightly and have it working now, and it’s picking up all my fields on the people page.

    Thanks for you help Michael ??

    • This reply was modified 7 months, 4 weeks ago by tonyp83.
    Thread Starter tonyp83

    (@tonyp83)

    Thanks @tw2113,

    I’ve updated my code to include all the fields (not just the field ‘short_introduction’) and it doesn’t seem to work.

    I’ve updated my code based on your suggestion to the below but it isn’t working. This is:

    function my_post_attributes( array $attributes, WP_Post $post ) {
        if ( 'people' !== $post->post_type ) {
            return $attributes;
        }
    
        $attributes['short_introduction'] = get_field( 'short_introduction', $post->ID );
    
        return $attributes;
    }

    Which is indexing short_introduction only. However, I want to index several fields and not just that.

    I’ve tried the below based on your suggestion but it doesn’t seem to work. I feel I’m maybe merging two methods.

    function my_post_attributes( array $attributes, WP_Post $post ) {
        if ( 'people' !== $post->post_type ) {
    		$fields = [
    			'short_introduction',
    			'skills_experience',
    			'awards',
    			'memberships',
    			'practice_areas',
    			'qualifications',
    			'experience',
    		];
    
    		$ourdata = [];
    		foreach ( $fields as $field ) {
    			$data = get_field( $field, $post->ID );
    			if ( ! empty( $data ) ) {
    				$ourdata[] = $data;
    			}
    		}
        }
    
        $attributes['people_fields'] = $ourdata;
    
        return $attributes;
    }
    
    
    /**
     * @param array $settings
     *
     * @return array
     */
    
    function my_posts_index_settings( array $settings ) {
    
    	$settings['searchableAttributes'][] = 'unordered(people_fields)';
    
    	return $settings;
    }
    
    add_filter( 'algolia_posts_people_index_settings', 'my_posts_index_settings' );
    
    • This reply was modified 7 months, 4 weeks ago by tonyp83.
    • This reply was modified 7 months, 4 weeks ago by tonyp83. Reason: tidied code
    Thread Starter tonyp83

    (@tonyp83)

    EDIT: Not sure what I’ve done and changed, but it seems to be working now. I can search certain words in the field “Short Introduction” and they’re showing in results!

    Thank you for your help and showing that update from speaker -> people. That must have been the fix.

    One question I have is, is it possible to list out numerous fields to search in this way, like an example where I can add 8 different fields all on the People post type. So essentially $attributes['short_introduction'] = get_field( 'short_introduction', $post->ID ); in an array?

    I feel everything is right with my functions file, and I’ve been pushing settings anytime I change anything, so I’m guessing this part is where I’m making a mistake. The amount of fields I can see in Searchable Attributes is always staying the same.

    If you have the custom fields properly indexed as attributes, they should be suggested with enough typing.

    I’m wondering whether I’ve missed something obvious.

    • This reply was modified 8 months ago by tonyp83.
    Thread Starter tonyp83

    (@tonyp83)

    I’ve updated what I’ve tried and this is now in my functions.php. I’m just trying to get the ACF field ‘short_introduction’ as the first field I want to index to work, which is only on the custom_post_type “People”.

    The way I’ve created my Fields is set a location rule in ACF, which is:

    Show this field group if
    Post Type > is equal to > Person

    I’m not sure if that will have any impact on what I’m trying to do.

    /**
     * @param array   $attributes
     * @param WP_Post $post
     *
     * @return array
     */
    
    function my_post_attributes( array $attributes, WP_Post $post ) {
    	if ( 'people' !== $post->post_type ) {
    		// We only want to add an attribute for the 'speaker' post type.
    		// Here the post isn't a 'speaker', so we return the attributes unaltered.
    		return $attributes;
    	}
    
    	// Get the field value with the 'get_field' method and assign it to the attributes array.
    	// @see https://www.advancedcustomfields.com/resources/get_field/
    	$attributes['short_introduction'] = get_field( 'short_introduction', $post->ID );
    
    	// Always return the value we are filtering.
    	return $attributes;
    }
    
    add_filter( 'algolia_post_shared_attributes', 'my_post_attributes', 10, 2 );
    add_filter( 'algolia_searchable_post_shared_attributes', 'my_post_attributes', 10, 2 );
    
    
    /**
     * @param array $settings
     *
     * @return array
     */
    
    function my_posts_index_settings( array $settings ) {
    
    	$settings['searchableAttributes'][] = 'unordered(short_introduction)';
    
    	return $settings;
    }
    
    add_filter( 'algolia_posts_speaker_index_settings', 'my_posts_index_settings' );
    
    Thread Starter tonyp83

    (@tonyp83)

    Thanks for the reply.

    I’m trying to add that via the Configuration in my Algolia Dashboard and can see the options to add, but the options it gives me when I click “Add a Searchable Attribute” don’t seem to have any options relating to what I’ve done, so I’m guessing I’ve missed a step.

Viewing 15 replies - 1 through 15 (of 28 total)