• Resolved hazardluxian

    (@hazardluxian)


    I would like to NOT show the testimonials in the search index. I already google it and found the code below, which 170 is the category’s ID that i made and put all the testimonials inside.

    But it doesn’t work.

    function searchcategory($query) {
    if ($query->is_search) {
    $query->set(‘cat’,’-170′);
    }
    return $query;
    }
    add_filter(‘pre_get_posts’,’searchcategory’);

    How can i remove all the testimonials from the search results?

    Thanks! Great plugin.

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi
    Do you use the plugin Yoast SEO ?
    If yes, then you might use that plugin to configure testimonials not to index.
    Go to SEO – Search Apperance – Content Types
    There you will see the usual content types like posts and pages but Testimonials as well
    Change ‘Show testimonials in Search Results’ to NO and save the changes

    • This reply was modified 5 years, 4 months ago by hugowachters.
    Thread Starter hazardluxian

    (@hazardluxian)

    Hello @hugowachters ty for the reply.

    I had installed, but not activated.

    Curious enough, the testimonials page wasn’t working, despite showing in the search, when i clicked i was redirected to a 404 page. I wasn’t worried about that problem because i didn’t intend to have people to access each separate testimonial, just to see everything in a list, so since i was trying to remove the testimonials from the search, it wouldn’t matter that each testimonial didn’t get a page of its own.

    Now i activated Yoast SEO and the pages work, so i can access each individual testimonial as a standalone post, but the search continues to show them even after configuring like you suggested. I turned off all the configuration regarding “wp_testimonials” from Yoast.

    Any ideas?

    Hello @hazardluxian,

    Thank you for reaching out to us!
    Could you please try and add this code in your theme’s functions.php file?
    add_filter( ‘wpmtst_exclude_from_search’, ‘__return_true’ );

    This should do it.
    Please let me know how it goes.

    All the best,
    Mihaela

    Thread Starter hazardluxian

    (@hazardluxian)

    @mplusb this code didn’t work and now my search doesn’t work anymore. Even after i removed the code, the search is now broken and no results are shown.

    How can this be? I understand just a little of coding and could be something in cache? I turned off Yoast as our friend above suggested and still the search doesn’t work.

    Please help!

    Thread Starter hazardluxian

    (@hazardluxian)

    @mplusb i had to get a backup of functions.php because your code messed up the file completely.

    The right functions.php has 14kb and when i applied this code it changed the file to 8kb erasing a lot of lines.

    If i didn’t had a fresh backup, this could have been catastrophic. I just double checked and it happened as soon as i save the file and upload to the server.

    I don’t have the knowledge to identify what this code should do or why it did what it did, but when applied my admin login page wouldn’t load, my search stopped working and my website stopped opening without using https:// before the domain’s address. This and other random problems.

    Hello @hazardluxian,

    I apologize for the inconvenience!
    I tested before-hand and added that same code in my theme’s functions.php and it should not have done what you experienced.
    Did you delete any other codes from that file? Where did you add it exactly? It needs to be added at the bottom of the file.

    I will look at another solution for this and get back to you.

    Thank you for being so patient,
    Mihaela

    Please do another back-up for your functions.php and in the old one add this code:

    add_action( 'init', 'exclude_testimonials_from_search', 99 );
    function exclude_testimonials_from_search() {
        global $wp_post_types;
    
        if ( post_type_exists( 'wpm-testimonial' ) ) {
            // exclude from search results
            $wp_post_types['wpm-testimonial']->exclude_from_search = true;
        }
    }
    

    Please add it at the end of that file and without removing anything from it.

    Thank you,
    Mihaela

    Thread Starter hazardluxian

    (@hazardluxian)

    @mplusb that worked!

    Regarding that other code, i didn’t removed any other line and put at the bottom of the file. As i said, i don’t know how to code, but i understand the basics and i was really surprised to see that code removing a lot of lines as it did.

    I repeated the whole process using the backup file just to make sure it was the code that was doing that and not something i did and indeed as i put that code, the file loses half the lines.

    But now is working! Thanks!

    Happy to hear it worked! Great job!
    And thank you for the feedback regarding the other code – it was really weird behavior and I was unable to replicate it.

    Thank you,
    Mihaela

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Exclude testimonials from search’ is closed to new replies.