Support

Account

Home Forums ACF PRO Problem with search title in PostObject

Solved

Problem with search title in PostObject

  • I have a problem when I want to enable searching by title or ‘content_shop_api_key’; I simply can’t search by title. When I disable that search of title, the code works without any issues. The ACF field ‘postObject’ is located in the User section. Please, if can someone give me some advice. This is my code:

    function custom_acf_post_object_query( $args, $field, $post_id ) {
        // Get the search text
        $the_search = $args['s'];
    
        // Remove it so ACF won't search the posts based on title
        unset($args['s']);
    
        // Search based on custom fields only
        $meta_query_args = array(
            'relation' => 'AND', 
            array(
                'relation' => 'OR', 
                array(
                    'key'     => 'post_title', 
                    'value'   => $the_search,
                    'compare' => 'LIKE'
                ),
                array(
                    'key'     => 'free_content_check',
                    'compare' => 'LIKE',
                    'value'   => $the_search,
                )
            ),
            array(
                'key'     => 'content_shop_api_key',
                'value'   => $the_search,
                'compare' => 'LIKE'
            )
        );
    
        $args['meta_query'] = $meta_query_args;
    
        return $args;
    }
    
    add_filter('acf/fields/post_object/query/name=assign_user_product', 'custom_acf_post_object_query', 10, 3);
  • EDIT:
    Code I posted is bad, this is my code:

    function custom_acf_post_object_query( $args, $field, $post_id ) {
        // Get the search text
        $the_search = $args['s'];
    
        // Remove it so ACF won't search the posts based on title
        unset($args['s']);
    
        // Search based on custom fields only
        $meta_query_args = array(
            'relation' => 'AND', 
            array(
                'relation' => 'OR', 
                array(
                    'key'     => 'post_title', 
                    'value'   => $the_search,
                    'compare' => 'LIKE'
                ),
                array(
                    'key'     => 'content_shop_api_key',
                    'value'   => $the_search,
                    'compare' => 'LIKE'
                )
                
            ),
           array(
                    'key'     => 'free_content_check',
                    'compare' => 'LIKE',
                    'value'   => $the_search,
                )
        );
    
        $args['meta_query'] = $meta_query_args;
    
        return $args;
    }
    
    add_filter('acf/fields/post_object/query/name=assign_user_product', 'custom_acf_post_object_query', 10, 3);
  • Do you have an ACF field named “post_title”, if not and this is referring to the WP post_title then this is not a meta value and it cannot be searched with a meta query.

    When WP does a search is breaks the search string down into individual words and adds multiple “LIKE” clauses, 1 for each word.

    
    search string = "this is a search"
    LIKE "%this%"
    LIKE "%is%"
    LIKE "%a%"
    LIKE "%search%"
    

    Like queries require enclosing in % characters.

  • Hi John, first thank you a lot for replay, this is very important to me.
    Well, this is custom post type, and I don’t have that field named ‘post_title’, I used WP post_title(default title). So, how i can implement search based on title in same code?

  • There isn’t a way to do the search that you are looking for, at least not easily.

    Normally when searching by post title and a meta query this is “AND” meaning that both the title AND the meta value must contain what you are searching for.

    There isn’t an easy way to do a search where the “post_title” OR the “meta_key” matches. Honestly, I haven’t a clue how to do this.

    What I would do is I would be to copy the post title to a meta value using an acf/save_post action. Then you can that meta key for searching. As far as breaking down the search string into words, I don’t know how to do that.

  • One more time, thank you a lot. At least i know what i can’t do. Now I will try to copy the post title to a meta value.

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

You must be logged in to reply to this topic.