Created
February 11, 2012 23:01
-
-
Save mboynes/1804885 to your computer and use it in GitHub Desktop.
Followup answer re: http://wordpress.stackexchange.com/questions/41667/most-efficient-way-to-search-for-values-from-cpt-in-post-content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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