Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #884791

    Hi,

    I am using the plug-in “Ultimate Category Excluder” to hide a specific category from the feed, page and archives.
    Unfortunately the plug-in is not working for the search (although there is this option in the settings).

    How can I exclude this category from the search?
    Furthermore there is a specific page I would like to exclude from the search as well, how can I do that?

    I already tried the following Functions.php code, which I found in this thread, but instead of excluding the categories with the id 5 an 9, it seems that it only includes these categories.

    function avia_exc_post($query) {
        if ($query->is_search) {
            $query->set('cat','5,9');
        }
        return $query;
    }
    add_filter('pre_get_posts','avia_exc_post');

    I am looking forward for your input.
    BR
    Florian

    #885466

    Hey Florian,

    Try like this

    
    function avia_exc_post($query) {
        if ($query->is_search) {
            $query->set( 'category__not_in', array(5, 9) );
        }
        return $query;
    }
    add_filter('pre_get_posts','avia_exc_post');
    

    If you need further assistance please let us know.
    Best regards,
    Victoria

    #885564

    Thanks a lot Victoria!!
    That worked perfectly for the categories I want to exclude!

    And how can I exclude specific pages (with id) from the search?

    BR
    Florian

    #886313

    Hi,

    Here is the first Google Result
    http://www.wpbeginner.com/wp-tutorials/how-to-exclude-pages-from-wordpress-search-results/

    Best regards,
    Basilis

    #886422

    Hi Basil,

    the code in your link is aiming to exclude all pages from WordPress search results, what I want to do is to exclude specific pages (by id).

    And I already tried several things I found via google such as:

    https://wp-dreams.com/forums/topic/exclude-specific-pages-in-search-results/

    but non of them worked. Don’t know why, if you have an explanation I am happy to hear it.

    The only thing I found which works is this plug-in , but I actually would prefer a php code, since I never know how plug-ins affect my page.
    So if you have a solution I am happy to hear it.

    BR

    #886447

    Hi catchbudapest,

    Plugins are php code also and you can see the plugin code as well. While looking at it, you might find an idea or an answer.

    Best regards,
    Victoria

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.