Support

Account

Home Forums ACF PRO Search in post object field by post_id

Solving

Search in post object field by post_id

  • 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.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.