I am desperately trying to do something that seems like a simple fix. I am using the Post Object field throughout a data-heavy website to connect to existing posts. I would like the search to query and return ONLY posts where the search string is like the post_title. It instead is searching post meta as well, including post content, and therefore returning a load of irrelevant results (searching through 45,000+ posts).
Here is my existing attempt to search only against the post_title (you’ll see I’d tried a couple methods):
// exclude drafts from ACF post object article dropdown, order by relevance, and return posts less than 5 years old
function clean_post_object_filter( $args, $field, $post_id )
{
// Search based on custom field (in this case ONLY post_title)
//$args[‘key’] = ‘post_title’;
//$args[‘value’] = $args[‘s’];
//$args[‘compare’] = ‘LIKE’;
$args[‘meta_query’] = array(
‘key’ => ‘post_title’,
‘value’ => $args[‘s’],
‘compare’ => ‘LIKE’
);
$args[‘post_status’] = array(‘publish’);
$args[‘orderby’] = ‘relevance’;
$args[‘order’] = ‘DESC’;
// this will exclude posts older than 5 years
$args[‘date_query’] = array(
‘after’ => date(‘Y-m-d G:i:s’, strtotime(‘-5 year’)),
‘inclusive’ => true
);
return $args;
}
// filter from every post object field
add_filter(‘acf/fields/post_object/query’, ‘clean_post_object_filter’, 10, 3);
I don’t know why it is searching meta values as well as the title and content. This usually requires a plugin or adding code to the WP search query to include meta values. Whatever is causing this it is not ACF.
post_content and post_title are not meta values and are stored in the post table.
ACF uses a standard WP search query to return values when searching. This automatically searches the title and content. You would need to alter this query using the filter you suggest. There are examples of this, for example this one https://stackoverflow.com/questions/9468804/make-wordpress-search-only-in-post-title. All of the examples require altering the actual SQL query and not the WP_Query().
Hi John, thanks for your reply. I misspoke. It’s not searching meta but rather post_content, and I ONLY want it to search post_title.
Can I alter the query to only apply to ACF cases (acf/fields/post_object/query)? I certainly don’t want to alter it sitewide.
Yes,
The way I would do this is first add a filter for ACF, in that filter I would add the filter than modifies the query, then after modifying the query I would remove the filter something like this.
add_filter('acf/fields/post_object/query/name=your-field-name', 'modify_my_field_name_query', 10, 3);
function modify_my_field_name_query($args, $field, $post_id) {
//using the example from the original link I posted
add_filter('posts_search', '__search_by_title_only', 500, 2);
return $args
}
function __search_by_title_only($search, $wp_query) {
remove_filter('posts_search', '__search_by_title_only', 500, 2);
/// modify the query as shown in the other post
return $search
}
Apologies if this is a duplicate reply. The system hasn’t accepted previous submits.
__
I instead chose to run the __search_by_title_only function only when a user is logged in, which satisfies my requirement.
Here’s the exact code I used:
function __search_by_title_only( $search, $wp_query )
{
global $wpdb;
$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->posts.post_title LIKE ‘{$n}{$term}{$n}’)”;
$searchand = ‘ AND ‘;
}
if ( ! empty( $search ) ) {
$search = ” AND ({$search}) “;
}
return $search;
}
// call this function ONLY if user is logged in (e.g. admin, editor)
if ( is_user_logged_in() ) {
add_filter(‘posts_search’, ‘__search_by_title_only’, 500, 2);
}
FYI:
I omitted the “&” from the 2nd parameter because it was throwing errors sitewide: “Parameter 2 to __search_by_title_only( ) expected to be a reference”
Produced errors:
function __search_by_title_only( $search, &$wp_query )
Working:
function __search_by_title_only( $search, $wp_query )
The topic ‘Query only against post_title in acf/fields/post_object/query’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.