With this line of code, we can pull our titles of a product if it has a matching SKU that we search for.
How can I pull the date the product was posted within this?
/**
* DFL Fire off Product relationship field – include searching SKU – Product Codes
*
* @param Array $args
* @param Array $field
* @param Integer $post_id
*
* @return $args
*/
function prefix_product_relationship_query_mods( $args, $field, $post_id ) {
$args[‘post_status’] = ‘publish’;
if( ! empty( $args[‘s’] ) ) {
$args[‘acf_sku_search’] = true;
}
return $args;
}
add_filter( ‘acf/fields/relationship/query/name=b_number_checker’, ‘prefix_product_relationship_query_mods’, 10, 3 );
/**
* Modify the query clauses to search on the postmeta table when asked to 🙂
*
* @param Array $clauses
* @param WP_Query $query
*
* @return Array $clauses
*/
function prefix_acf_relationship_search_sku( $clauses, $query ) {
global $wpdb;
if( $query->get( ‘acf_sku_search’, false ) ) {
$clauses[‘join’] = ” LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id “;
$clauses[‘where’] = preg_replace(
“/\(\s*{$wpdb->posts}.post_title\s+LIKE\s*(\'[^\’]+\’)\s*\)/”,
“({$wpdb->posts}.post_title LIKE $1) OR ({$wpdb->postmeta}.meta_key = ‘_sku’ AND {$wpdb->postmeta}.meta_value LIKE $1)”,
$clauses[‘where’]
);
$query->set( ‘acf_sku_search’, false ); // Set to false just in case.
}
return $clauses;