0

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.

1 Answer 1

0

Welcome to the community :)

Generally speaking, the WP_Query is quite a central piece of everything in Wordpress, so I'd recommend first getting a solid understanding of what is going on inside the core before tinkering too much with WP_Query as it will often have undesired side-effects which are hard to understand & debug.

Having that said, I see where you want to go and in a specific use-case your solution might do just fine.

I haven't tested your code but it should work (well, at least affect the search) if you change this:

wpse342309_search_terms( $query, 'help_cat' );

to this:

$query = wpse342309_search_terms( $query, 'help_cat' );

Two more points:

  1. Are you aware that the solution will also affect search results in the frontend?

  2. Also, the solution doesn't respect sort order, which means that no matter how the results are sorted, the terms will always come first, always in the same order.

5
  • Hey. Thanks for the response. I've tried your suggestion, but it doesn't seem to work either. If I want to only affect the editor , should I wrap my codeblock from filter_search inside a if(is_admin())? Also, I have ~1 month since I started working in WP, what would you recommend to use to test out stuff like WP_Query?Is there a playground where I could test this out?
    – tearswep
    Commented Aug 17, 2022 at 11:39
  • imgur.com/a/tRrzTDg , this is my trying to access FAQ taxonomy in the editor, but no results are showing
    – tearswep
    Commented Aug 17, 2022 at 11:41
  • I've changed this if($query->is_main_query() && $query->is_search()) to if($query->is_search() && is_admin()) and it does seem to affect the query(in the sense that if i change $query->set('post_type', array(post')) it displays only blog posts in the editor), but it doesn't really show taxonomies to show. Is there an easier way to debug stuff like this in WP?
    – tearswep
    Commented Aug 17, 2022 at 12:05
  • Sorry, I forgot to get back to this. There are not really any playgrounds that I know of. You need at least basic understanding of PHP, a proper IDE like PhpStorm or VSCode and a debugger like Xdebug to be able to navigate the code and understand the request lifecycle, where WP_Query plays a significant role. So no quick paths, AFAIK ;) On the other side, it's challenges just like yours that push you forward and make you learn new things.
    – jgangso
    Commented Aug 25, 2022 at 12:06
  • Unfortunately I don't have a environment right now to check closer to your issue. :/
    – jgangso
    Commented Aug 25, 2022 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.