filters – Amend taxonomy to search in wordpress admin

Question

I am trying to amend a custom taxonomy to a search in the editor area.
The taxonomy slug is ‘help_cat’ and is attached to ‘help-center’ post types.

What I am trying to achieve is very similar with OP from this post:

display taxonomy terms in results

My WP setup : Multisite, Oxygen Builder.

The code that I’ve included in Code Snippets :

function wpse342309_search_terms( $query, $taxonomy ) {
    $per_page = absint( $query->get( 'posts_per_page' ) );
    if ( ! $per_page ) {
        $per_page = max( 10, get_option( 'posts_per_page' ) );
    }

    $paged = max( 1, $query->get( 'paged' ) );
    $offset = absint( ( $paged - 1 ) * $per_page );
    $args = [
        'taxonomy'   => $taxonomy,
//      'hide_empty' => '0',
        'search'     => $query->get( 's' ),
        'number'     => $per_page,
        'offset'     => $offset,
    ];

    $query->terms = [];
    $terms = get_terms( $args );
    if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
        $query->terms = $terms;
    }

    $args['offset'] = 0; // always 0
    $args['fields'] = 'count';
    $query->found_terms = get_terms( $args );

    $query->term_count = count( $query->terms );
    $query->terms_per_page = $per_page; // for WP_Query::$max_num_pages
    $query->is_all_terms = ( (int) $per_page === $query->term_count );

    $query->set( 'posts_per_page', max( 1, $per_page - $query->term_count ) );
    $query->set( 'offset', $query->term_count ? 0 :
        max( 0, $offset - $query->found_terms ) );
}

function filter_search($query) {
    // Limit search to posts
    if($query->is_main_query() && $query->is_search()) {
        $query->set('post_type', array('help-center', 'post', 'activities', 'page'));
        wpse342309_search_terms( $query, 'help_cat' );
    }

    // Return query
    return $query;
  
}



add_filter('pre_get_posts', 'filter_search');

This, however doesn’t seem to affect the search at all.
I am fairly new to WordPress and I’m still trying to piece everything together, but I am not sure where I am going wrong with this. Any help/guidance would be much appreciated.

0
tearswep 1 month 2022-08-17T04:11:41-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse