Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synonyms: ignore ep-synonym post status #2517

Merged
merged 3 commits into from
Dec 14, 2021
Merged

Conversation

felipeelia
Copy link
Member

Description of the Change

This PR changes the WP_Query to fetch ep-synonyms, getting all synonyms with any post status. It also makes the check a little looser, simply checking it the post count is greater than or equal to 1, in case someone is changing posts_per_page in all queries.

Applicable Issues

Closes #2516

Changelog Entry

Fixed: Synonyms erased when syncing via WP-CLI. Props @felipeelia and @oscarssanchez

@felipeelia
Copy link
Member Author

Users facing this problem can fix it by adding the following snippet to a custom plugin or their theme's functions.php:

add_action(
	'pre_get_posts',
	function ( $query ) {
		$post_type = (array) $query->get( 'post_type', [] );
		if ( in_array( 'ep-synonym', $post_type, true ) ) {
			$query->set( 'post_status', 'any' );
		}
	}
);

@felipeelia felipeelia mentioned this pull request Dec 13, 2021
oscarssanchez
oscarssanchez previously approved these changes Dec 13, 2021
@felipeelia
Copy link
Member Author

The following snippet can be used to delete all synonyms created by that bug and bring back the last edited synonyms list. It can be used by adding it to the codebase, opening any page, and then erasing it (it only needs to be executed once.)

add_action(
	'init',
	function() {
		global $wpdb;

		$synonyms = \ElasticPress\Features::factory()->get_registered_feature( 'search' )->synonyms;

		$synonyms_example_ids = $wpdb->get_col(
			$wpdb->prepare(
				"SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_content = %s LIMIT 500",
				$synonyms::POST_TYPE_NAME,
				$synonyms->example_synonym_list()
			)
		);

		if ( ! $synonyms_example_ids ) {
			return;
		}

		foreach ( $synonyms_example_ids as $synonym_post_id ) {
			wp_delete_post( $synonym_post_id, true );
		}
	}
);

@mckdemps mckdemps modified the milestones: 4.0.0 (beta 2), 3.6.6 Dec 14, 2021
@felipeelia felipeelia merged commit 4fe6a5c into develop Dec 14, 2021
@felipeelia felipeelia deleted the fix/synonyms-and-cli-index branch December 14, 2021 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Synonyms that were set before do not show up in synonym dashboard after a WP CLI reindex
3 participants