• Resolved thegraematter

    (@thegraematter)


    Hi there.

    I run a listings website where posts have an expiry date and then will default into an archive category. For example a theatre show listing ends on Sat 29th October. It is in the category for Theatre, This Week. At a preset time say 23:50 on 29th October it will then recategorise to only the Archive category.

    Is there a way of being able to engage ‘search exclude’ for the Archive category ONLY so that every post in it currently and in the future will be excluded from visitor searches (but not exclude it from search engines).

    As I see it I would have to go through my archive file every day and check the exclude box which is going to be very time consuming.

    Thanks in advance

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor pronskiy

    (@pronskiy)

    Hi,

    Let me think about it and I’ll come back to you soon.

    Roman

    Plugin Contributor pronskiy

    (@pronskiy)

    Hi,

    Here is one simple solution.
    Add the following code to your theme’s functions.php file:

    
    add_filter('searchexclude_filter_search', 'archiveCategoryFilter', 10, 2);
    function archiveCategoryFilter($exclude, $query)
    {
        $category__not_in = $query->get('category__not_in');
        $query->set('category__not_in', array_merge($category__not_in, array(get_cat_ID('Archive'))));
    	return $exclude;
    }
    

    Depending on your site load, it is possible to optimize the solution, if you need that.

    Please let me know if this works for you.
    And feel free to contact me by email for any help: [email protected]

    Cheers!
    Roman

    Thread Starter thegraematter

    (@thegraematter)

    Hi Roman,

    Thank you for looking for a solution. Unfortunately with this code in place it is still searching and including posts from the archive category.

    Graeme

    Plugin Contributor pronskiy

    (@pronskiy)

    Hi Graeme,

    That could happen for many reasons. Please contact me by email [email protected] so that we could resolve this faster.

    Roman

    Plugin Contributor pronskiy

    (@pronskiy)

    After some debugging we were able to find the working solution.

    Need to add following code into theme’s functions.php:

    
    function archiveCategoryFilter($query)
    {
        $exclude =
                (!is_admin() || (defined('DOING_AJAX') && DOING_AJAX))
                && $query->is_search;
    
        if ($exclude) {
        	$category__not_in = $query->get('category__not_in');
            $query->set('category__not_in', array_merge($category__not_in, array(get_cat_ID('archives'))));
        }
    
        return $query;
    }
    add_filter('pre_get_posts', 'archiveCategoryFilter');
    

    NOTE: It does not require the SearchExclude plugin to be installed.

    • This reply was modified 7 years, 12 months ago by pronskiy.

    @pronskiy, I normally try not to add my issues/request to other people’s threads; however, in this case I came here looking exactly for this and I felt it would probably be more efficient to add the request here instead of starting a new topic. If you wish me to do that, I would be happy to split it off.

    I was wondering, since you have done such an excellent job with this; I installed it today and so far doing great. The interface to quick edit the option into pages, was genius, thank you for that – I was pleasantly surprised to see it there.

    So naturally, we want more ?? I know we can be so selfish. But honestly I feel that it would make this plugin even more amazing, and I hope you will consider it. Can you please add a similarly genius interface for setting the excludes on the category page as well. So basically like the posts/pages, we could simply load the category page and somewhere next to each item have an exclude and once saved, it will automatically do what is being done manually in the code you provided above?

    I think ultimately many would prefer not having to manually modify their theme/etc files and let the plugin manage it more efficiently and centralized to prevent fragmentation of solutions. Thank you in advance.

    • This reply was modified 7 years, 8 months ago by Gμ?rD???. Reason: wrong attribution
    Plugin Contributor pronskiy

    (@pronskiy)

    Hi @guardian74,

    Glad you liked the plugin!
    Thanks for describing the idea in details. It actually was on my radar for a while, but I was postponing it. Looks like now is the time to implement it ??
    So far I can not provide an estimate, let me think about this and I’ll get back to you soon.

    @pronskiy thank you and I look forward to it. If anything I can help with or in the least can help test, let me know please.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘how do I exclude a category and the posts with in it’ is closed to new replies.