Support

Account

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

Solved

Select 2 Search Broken w/ Title Mods

  • I’m using the acf/fields/post_object/result filter to completely modify the title of the post. It’s querying WooCommerce Shop Orders and the titles do not quite match what WooCommerce shows on their orders page so I’m making them match for consistency. The issue is that by doing so it completely break the Select2 search functionality. I can search on the old titles but not on the new titles.

    Assuming I know development, where should I be looking to update the select2 ajax search results so that it searches on the updated titles?

  • What is search for is controlled by WP_Query() ‘s’ argument. https://developer.wordpress.org/reference/classes/wp_query/#search-parameters. This causes WP to search the title and content of the post. In order to search something else you must actually alter the way WP searches posts. http://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/

  • 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

  • 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.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Select 2 Search Broken w/ Title Mods’ is closed to new replies.