I've used the following filter to include an ACF field into my search results.
add_filter( 'pre_get_posts', 'so_9224493_adjust_search_query');
function so_9224493_adjust_search_query( $query ) {
if ( !is_admin() && $query->is_search ) { //the !is_admin() boolean ensures that this does not affect your dashboard
$meta_query_arguments = array(
array(
'key' => 'item_number', //this is where you would put your meta key
'value' => $query->query_vars['s'],
'compare' => 'LIKE',
),
);
$query->set('meta_query', $meta_query_arguments);
};
}
Sadly, it doesn't work. However, if it did work, I want to restrict the search to that field only. I don't want the title or content etc. of posts and pages.
Along the process, I stumbled across a more promising filter, so I've tried adapting it to my needs.
function ni_search_by_title_only( $search, &$wp_query ){
global $wpdb;
if ( empty( $search ) )
return $search;
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search =
$searchand = '';
foreach ( (array) $q['search_terms'] as $term ) {
$term = esc_sql( like_escape( $term ) );
$search .= "{$searchand}($wpdb->postmeta.meta_value LIKE '{$n}{$term}{$n}')";
$searchand = ' AND ';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = '') ";
}
return $search;
}
add_filter( 'posts_search', 'ni_search_by_title_only', 500, 2 );
This doesn't work. Ideally, it would NOT search all meta_value
fields, but only those of a specify meta_key
.
PS: I'm not interested in a plugin to achieve what I'm after
I've used the following filter to include an ACF field into my search results.
add_filter( 'pre_get_posts', 'so_9224493_adjust_search_query');
function so_9224493_adjust_search_query( $query ) {
if ( !is_admin() && $query->is_search ) { //the !is_admin() boolean ensures that this does not affect your dashboard
$meta_query_arguments = array(
array(
'key' => 'item_number', //this is where you would put your meta key
'value' => $query->query_vars['s'],
'compare' => 'LIKE',
),
);
$query->set('meta_query', $meta_query_arguments);
};
}
Sadly, it doesn't work. However, if it did work, I want to restrict the search to that field only. I don't want the title or content etc. of posts and pages.
Along the process, I stumbled across a more promising filter, so I've tried adapting it to my needs.
function ni_search_by_title_only( $search, &$wp_query ){
global $wpdb;
if ( empty( $search ) )
return $search;
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search =
$searchand = '';
foreach ( (array) $q['search_terms'] as $term ) {
$term = esc_sql( like_escape( $term ) );
$search .= "{$searchand}($wpdb->postmeta.meta_value LIKE '{$n}{$term}{$n}')";
$searchand = ' AND ';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = '') ";
}
return $search;
}
add_filter( 'posts_search', 'ni_search_by_title_only', 500, 2 );
This doesn't work. Ideally, it would NOT search all meta_value
fields, but only those of a specify meta_key
.
PS: I'm not interested in a plugin to achieve what I'm after
Share Improve this question asked Jun 14, 2019 at 7:45 idlebergidleberg 1576 bronze badges1 Answer
Reset to default 0I ended up using a simple meta_query
:
$args = [
'post_status' => 'publish',
'post_type' => $post_type,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'my_acf_field',
'value' => $search_term,
'compare' => 'LIKE'
),
)
];
$query = new WP_Query($args);