Skip to content

Instantly share code, notes, and snippets.

@mboynes
Created February 11, 2012 23:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mboynes/1804885 to your computer and use it in GitHub Desktop.
Save mboynes/1804885 to your computer and use it in GitHub Desktop.
<?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