• Resolved johnwiggity

    (@johnwiggity)


    Hi I was wondering if it’s possible to exclude a WordPress category from the Posts collection.

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Sachyya

    (@sachyya-sachet)

    Hello @johnwiggity

    Hope you are doing well.

    One simple solution is to unassign the category you want to exclude from the posts and update the posts.

    Or you can use the below code in your child theme’s functions.php to exclude specific category:

    <?php
    function your_slug_exclude_category ( $formatted_data, $raw_data, $object_id, $schema_name ) {
         if ( $schema_name == 'post' ) {
    	     $categories     = get_the_category( $raw_data->ID );
    	     if ( ! empty( $categories ) ) {
    		     foreach ( $categories as $category ) {
    			     // Replace 54 with your category ID
    				 if( 54 == $category->term_id ) {
    					 $formatted_data['category'] = [];
    				 }
    		     }
    	     }
         }
         return $formatted_data;
     }
     add_filter( 'cm_typesense_data_before_entry', 'your_slug_exclude_category', 10, 4 );

    Please not you would have to index your posts again for this to take affect.

    Hope this helps. Please let us know if you need further assistance.

    Regards

    Thread Starter johnwiggity

    (@johnwiggity)

    Ok thanks so much! The plugin works great.

    Thread Starter johnwiggity

    (@johnwiggity)

    I had one other quick question. With the sorting, is the Recent based on recently updated posts? Is there a way to change that by post date?

    Plugin Contributor Sachyya

    (@sachyya-sachet)

    Hello @johnwiggity ,

    Glad it is working for you.

    The Recent sorting is based on post_date not on the post_modified date.

    You can change it by using the filter: cm_typesense_search_sortby_settingsor override the sort-by.php template from templates/partials/sort-by.php. Read more about overriding templates here: https://docs.wptypesense.com/template-overriding/

    Regards,

    Thread Starter johnwiggity

    (@johnwiggity)

    Ok thanks so much. The last thing I was wondering is if it’s possible to filter out any shortcodes that show up in the search results?

    Plugin Contributor Sachyya

    (@sachyya-sachet)

    Hello @johnwiggity ,

    To exclude the shortcodes from showing up in the search results, you can do it with one of the following approaches:


    1. Use the_content filter to strip out the shortcodes

    2. Use cm_typesense_data_before_entry filter to filter the content before indexing it.

    Hope this helps. Please get back to us if you need further assistance.

    Regards

    Thread Starter johnwiggity

    (@johnwiggity)

    Ok thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Exclude Category’ is closed to new replies.