## Display post titles that match string in post content

Question

I’m trying to search through the content of the current post and look for any strings in the content that match the title of any ‘term’ custom post types.

The idea is that any terms we have that are in the post description are right there for a user to view if they need clarification. This script works perfectly when I put in any string for the variable $content_search, but stops working properly when I set $content_search = get_the_content();


It spits out the matching terms, but also a few seemingly random ones as well.

I’ve been staring at this block of code for quite some time and I just can’t figure out what’s wrong. Any help would be awesome!

                    <?php

// search for matching glossary terms that are in the description

$content_search = get_the_content();$content_search = strtolower($content_search);$args_terms = array(
'post_type' => 'term',
'orderby'   => 'title',
'posts_per_page' => -1
);

$glossary_terms = new WP_Query($args_terms);

echo '<h3>Terms Used in this Lesson:</h3>';

while ($glossary_terms->have_posts()){$glossary_terms->the_post();
$tt_title = strtolower(get_the_title()); if (strpos($content_search, $tt_title) !== false){$tt_title = ucwords($tt_title);$tt_content = strip_tags(get_the_content());
echo do_shortcode('[tooltip tooltip_title="' . $tt_content . '"]' .$tt_title . '[/tooltip]');
echo ' | ';
}
}

wp_reset_postdata();
?>

0
9 years 2014-09-05T19:14:42-05:00 0 Answers 79 views 0