Skip to content

Instantly share code, notes, and snippets.

@mwhiteley16
Last active July 18, 2022 05:46
Show Gist options
  • Save mwhiteley16/af355c262381df5107261b79ca4e1004 to your computer and use it in GitHub Desktop.
Save mwhiteley16/af355c262381df5107261b79ca4e1004 to your computer and use it in GitHub Desktop.
Index FacetWP on Form Submission/Edit
<?php
/**
* Ensure FacetWP indexes when events are created/edited forms domain
*/
add_action( 'af/form/submission', function ( $form, $fields, $args ) {
if ( isset( AF()->submission['post'] ) ) {
$post_id = AF()->submission['post'];
FWP()->indexer->index( $post_id );
// tried to reindex all, same result
// FWP()->indexer->index();
}
}, 20, 3 );
// ANOTHER ATTEMPT
add_filter( 'af/form/editing/post_data', function ( $post_data, $form, $args ) {
if ( $form['key'] !== 'form_62c59a4801f0b' ) {
return $post_data;
}
FWP()->indexer->index( $post_data['ID'] );
// tried to reindex all, same result
// FWP()->indexer->index();
return $post_data;
}, 10, 3 );
// ATTEMPT # 3
add_action( 'af/form/editing/post_updated/key=form_62c59a4801f0b', 'wd_facetwp_index_on_event_update', 10, 3 );
add_action( 'af/form/editing/post_created/key=form_62c59a4801f0b', 'wd_facetwp_index_on_event_update', 10, 3 );
function wd_facetwp_index_on_event_update( $post, $form, $args ) {
// reindex FacetWP to ensure updated/created event is indexed properly
FWP()->indexer->index( $post->ID );
// also attemped a full reindex to no avail
FWP()->indexer->index();
}
@mishterk
Copy link

Hi @mwhiteley16,

I wonder if something as simple as this will do it:

add_action( 'init', function () {
	if ( ! empty( AF()->submission['post'] ) and is_int( AF()->submission['post'] ) ) {
		if ( empty( FWP()->indexer ) ) {
			trigger_error( 'WFP()->indexer instance was not available — could not trigger index on post IDs.' );
		}

		FWP()->indexer->index( AF()->submission['post'] );
	}
}, 30 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment