function add_remove_document_to_solr_index($post_id, $post, $update) { // If this is just a revision, don't go on. if (wp_is_post_revision($post_id)) { return; } // If this is just a new post opened in editor, don't go on. if ('auto-draft' == $post->post_status) { return; } try { if ('publish' == $post->post_status) { // post published, add/update it from Solr index $solr = WPSolrIndexSolrClient::create_from_post($post); $solr->index_data(1, $post); // Display confirmation in admin set_transient(get_current_user_id() . 'updated_solr_post_save_admin_notice', 'Post/page indexed in Solr'); } else { // post unpublished, remove it from Solr index $solr = WPSolrIndexSolrClient::create_from_post($post); $solr->delete_document($post); // Display confirmation in admin set_transient(get_current_user_id() . 'updated_solr_post_save_admin_notice', 'Post/Page deleted from Solr'); } } catch (Exception $e) { set_transient(get_current_user_id() . 'error_solr_post_save_admin_notice', htmlentities($e->getMessage())); } }
/** * Add/remove document to/from Solr index when status changes to/from published * We have to use action 'save_post', as it is used by other plugins to trigger meta boxes save * * @param $post_id * @param $post */ function add_remove_document_to_solr_index($post_id, $post) { // If this is just a revision, don't go on. if (wp_is_post_revision($post_id)) { return; } // If this is just a new post opened in editor, don't go on. if ('auto-draft' === $post->post_status) { return; } // Delete previous message first delete_transient(get_current_user_id() . 'updated_solr_post_save_admin_notice'); try { if ('publish' === $post->post_status) { // post published, add/update it from Solr index $solr = WPSolrIndexSolrClient::create_from_post($post); $results = $solr->index_data(1, $post); // Display confirmation in admin, if one doc at least has been indexed if (!empty($results) && !empty($results['nb_results'])) { set_transient(get_current_user_id() . 'updated_solr_post_save_admin_notice', sprintf('%s updated in index \'%s\'', ucfirst($post->post_type), $solr->index['index_name'])); } } else { // post unpublished, remove it from Solr index $solr = WPSolrIndexSolrClient::create_from_post($post); $solr->delete_document($post); // Display confirmation in admin set_transient(get_current_user_id() . 'updated_solr_post_save_admin_notice', sprintf('%s removed from index \'%s\'', ucfirst($post->post_type), $solr->index['index_name'])); } } catch (Exception $e) { set_transient(get_current_user_id() . 'error_solr_post_save_admin_notice', htmlentities($e->getMessage())); } }