Hi,
Is there a way to search the post object field by post id?
I have a post object field. I want to be able to write the post Id in the post_object search bar and retrieve the post with the corresponding id
I have added this code:
function add_post_object_id( $title, $post, $field, $post_id ) {
$titleid .= $post->ID .' - ' . $title;
return $titleid;
}
add_filter('acf/fields/post_object/result', 'add_post_object_id', 10, 4);
Using this code I can see the post id in the option search result, but when I place the post id in the search box I don’t get the right post.
Thanks
Do to this would require multiple filters.
The first would be by using acf/fields/post_object/query https://www.advancedcustomfields.com/resources/acf-fields-post_object-query/
This filter would need to add another filter on “posts_where” to alter the where portion of the query add searching the post ID, it is very similar to searching custom fields with the WP search. See this for more information https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/
I took me quite some time to figure out on how to do this – so I’ll post my solution to hopefully help somebody else who’s having the same Problem:
My requirement was to be able to search not only for the title but an ACF field (a number in my particular case) as well.
I stumbled across the second link from Johns answer before and was able to use it for the search functionality of the edit screens of my custom posts – but it was not working for the post object ACF field – since $is_search is not set on ajax requests/queries.
I tried to find another way to identify the query from the post object field and in the end I ended up with the following:
function cf_post_object_query( $args ) {
// Set a query variable to identify the post object query
$args['is_acf_query'] = true;
// return
return $args;
}
// filter for a specific field
add_filter('acf/fields/post_object/query', 'cf_post_object_query');
In combination with Adams filters (slightly customized):
function cf_search_where( $where, $wp_query ) {
global $pagenow, $wpdb;
$is_acf = isset( $wp_query->query['is_acf_query'] ) ? $wp_query->query['is_acf_query'] : false;
if ( is_search() || $is_acf ) {
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
}
return $where;
}
add_filter( 'posts_where', 'cf_search_where', 10, 2 );
(You’d need to do this for all three of them I suppose)
But it finally works, I can search for the acf field and the title in the post object field.
Hi. I was just about to do the same thing: allowing editors to search custom posts not only by titles, but also by a secondary title field.
However I am unable to make this work with the solution PeterSeb provided (also adding Join filter from the article). Looks like search for fields is completely ignored. Working if the query is run separately as a test, but not working here.
Update. The issue was that in-article Join statement was duplicating and not receiving “AS …” prefix. So had to manually add custom prefix to this new join (also in the Where query) and now working.
Thanks @peterseb for this solution. Worked for me, on a Post Object field. In my case I needed users (admins actually) to be able to search for a post by its ID. Here’s my code for that:
function cf_post_object_query( $args ) {
// Set a query variable to identify the post object query
$args['is_acf_query'] = true;
// return
return $args;
}
// filter for a specific field
add_filter('acf/fields/post_object/query', 'cf_post_object_query');
function cf_search_where( $where, $wp_query ) {
global $pagenow, $wpdb;
$is_acf = isset( $wp_query->query['is_acf_query'] ) ? $wp_query->query['is_acf_query'] : false;
if ( is_search() || $is_acf ) {
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
//"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->posts.".ID LIKE $1)", $where );
}
return $where;
}
add_filter( 'posts_where', 'cf_search_where', 10, 2 );
You must be logged in to reply to this topic.
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.