Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
add_action('publish_post', 'schedule_glossary_index', 10, 1);
function schedule_glossary_index($post_id) {
wp_schedule_single_event(time(), 'update_glossary_index', array( $post_id ));
}
add_action('update_glossary_index', 'do_update_glossary_index', 10, 1);
function do_update_glossary_index($post_id) {
global $wpdb;
$post_content = $wpdb->get_var("SELECT `post_content` FROM `{$wpdb->posts}` WHERE `ID`=$post_id");
$wpdb->query("DELETE FROM `{$wpdb->prefix}posts_glossary` WHERE `post_id`=$post_id");
$sql_regex = rtrim(preg_replace('/[\W\s]*(\w+)[\W\s]*/','$1|',$post_content),'|');
$wpdb->query(
"INSERT INTO `{$wpdb->prefix}posts_glossary` (`post_id`,`glossary_id`)
SELECT $post_id,`ID` FROM `{$wpdb->posts}` WHERE `post_status`='publish' AND `post_type`='glossary' AND `post_parent`>0 AND `post_title` REGEXP('^$sql_regex\$')"
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.