Custom field works but not included in search results
-
I have created a custom field for my shop using the standard WC hooks however I don’t know how to modify the search query so that searches also look in this custom field.
The working code to define and save my custom field is as follows…
// WC custom fields : define field
function custom_wc_fields_aj() {
$args = array(
‘label’ => __( ‘Product Variant Codes’, ‘woocommerce’ ),
‘placeholder’ => __( ‘Enter variant codes here (comma separated)’, ‘woocommerce’ ),
‘id’ => ‘aj_variant_codes’,
‘desc_tip’ => true,
‘description’ => __( ‘These extra variant codes are used for searches.’, ‘woocommerce’ ),
);
woocommerce_wp_text_input( $args );
}
add_action( ‘woocommerce_product_options_sku’, ‘custom_wc_fields_aj’, 20 );// save custom WC fields
function save_wc_fields_aj( $post_id ) {
// grab the custom SKU from $_POST
$custom_sku = isset( $_POST[ ‘aj_variant_codes’ ] ) ? sanitize_text_field( $_POST[ ‘aj_variant_codes’ ] ) : ”;
$product = wc_get_product( $post_id );
$product->update_meta_data( ‘aj_variant_codes’, $custom_sku );
$product->save();
}
add_action( ‘woocommerce_process_product_meta’, ‘save_wc_fields_aj’, 20);I’ve searched and tried a huge number of sample code items on modifying the WP query if its a search but none work. Does anyone have reference code that definitely works on current versions of WP ?
- The topic ‘Custom field works but not included in search results’ is closed to new replies.