Support

Account

Home Forums General Issues Select 2 Search Broken w/ Title Mods Reply To: Select 2 Search Broken w/ Title Mods

  • Thank you for your suggestion and pointing out how the Select2 handles search. I had just assumed Select2 was doing a text based search via JavaScript on the results. Knowing this, I went ahead an used the same code WooCommerce uses to search their shop_orders. The whole function is below:

    /**
    * WooCommerce Shop Order titles are not search friendly.
    * Use the same thing WooCommerce does to modify search.
    *
    * Taken straight from WooCommerce
    * woocommerce/includes/admin/list-tables/class-admin-list-table-orders.php
    * LN 870
    * FN search_custom_fields()
    *
    * @param WP_Query $wp
    *
    * @return void
    */
    function prefix_modify_shop_order_search_select( $wp ) {

    if( ! ( isset( $_REQUEST[‘action’] ) && ‘acf/fields/post_object/query’ == $_REQUEST[‘action’] ) ) { // Ensure we are in an ACF post_object query action
    return;
    } else if( ! ( isset( $_REQUEST[‘post_id’] ) && ‘options’ == $_REQUEST[‘post_id’] ) ) { // Ensure we’re in an option page ( Specific to my usecase )
    return;
    } else if( ! ( ‘shop_order’ == $wp->query_vars[‘post_type’] && ! empty( $wp->query_vars[‘s’] ) ) ) { // Ensure that the query is a shop_order query and search is not empty.
    return;
    }

    /**
    * All the below is WooCommerce Specific
    */
    $post_ids = wc_order_search( wc_clean( wp_unslash( $wp->query_vars[‘s’] ) ) ); // WPCS: input var ok, sanitization ok.

    if ( ! empty( $post_ids ) ) {
    // Remove “s” – we don’t want to search order name.
    unset( $wp->query_vars[‘s’] );

    // so we know we’re doing this.
    $wp->query_vars[‘shop_order_search’] = true;

    // Search by found posts.
    $wp->query_vars[‘post__in’] = array_merge( $post_ids, array( 0 ) );
    }

    }
    add_action( ‘parse_query’, ‘prefix_modify_shop_order_search_select’ );
    WooCommerce already has the query_vars in place and ready to be parsed. At this point WooCommerce does the heavy lifting. Valid as of WooCommerce version 3.8.0

    thanks my issue has been fixed.