1

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();
                        ?>

1 Answer 1

0

The problem is that you are matching the title string in the entire content as whole and not word by word. e.g. if your title is 'blog' and content contains the word 'blogging', it will still match and pull 'blog' from 'blogging'. So, you can compare them word by word by exploding the content as follows:

<?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());

                        $content = explode(" ", $content_search);

                        foreach($content as $term){

                            if ($term === $tt_title){

                                $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();
                        ?>
3
  • Thanks for the suggestion. That would narrow the results, but the problem is if there is a term with two words like 'apple juice' it won't find it because it won't match apple or juice. Using strpos it will find it. I see what you are saying though about blog and blogging, but I figure those are similar enough that if the term 'blogging' is in the content and it shows the term 'blog' that's fine. The problem is I have content like this: 'this is the content' and I get the terms 'content', 'this', 'monkey'. Where'd the term 'monkey' come from?
    – user57391
    Commented Sep 6, 2014 at 17:38
  • So upon closer inspection, get_the_content() displays the correct content, however when I do a character count on it, it says there are way more character than there actually are. About 5x as many. There's something hidden that the terms are being matched against.
    – user57391
    Commented Sep 6, 2014 at 18:11
  • 1
    Ok, I figured it out. A recent developer included the terms, but hid them inside the content with display:none... So they're there, just not visible. Now I have to figure out how to strip out all that garbage across 700+ posts.
    – user57391
    Commented Sep 6, 2014 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.