How to trigger FacetWP's indexer for posts managed by an ACF form

Created by Phil Kurth, Modified on Tue, 02 Aug 2022 at 08:08 AM by Phil Kurth

If you're using FacetWP, you might find the newly created or updated posts aren't being indexed as you would expect. 


The solution to this problem is via a simple code snippet that tells FacetWP to index any created/edited post object. You should be fine to place this snippet in a custom plugin, an MU plugin, or in your theme's functions.php file: 


<?php

add_action( 'init', function () {

	if ( ! function_exists( 'AF' ) ) {
		trigger_error( 'AF() function is unavailable — the Advanced Forms plugin may not be active.' );
		return;
	}

	if ( ! function_exists( 'FWP' ) ) {
		trigger_error( 'FWP() function is unavailable — the FacetWP plugin may not be active.' );
		return;
	}

	// Check for a modified post ID.
	if ( ! empty( AF()->submission['post'] ) and is_int( AF()->submission['post'] ) ) {

		// Make sure FacetWP's indexer is available.
		if ( empty( FWP()->indexer ) ) {
			trigger_error( 'FWP()->indexer instance was not available — could not trigger index on post IDs.' );
			return;
		}

		// Pass the indexer the post ID.
		FWP()->indexer->index( AF()->submission['post'] );

	}
}, 30 );

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article