Support

Account

Home Forums Backend Issues (wp-admin) ACF Page Link – search by title only Reply To: ACF Page Link – search by title only

  • ACF uses the WP built in search which searches the titles and content of posts. In order to search only titles you need to alter the way WP does searches. See this https://www.isitwp.com/limit-search-to-post-titles-only/

    To apply this to only one field in ACF

    
    add_fitler('acf/fields/page_link/query/name=YOUR_FIELD_NAME', 'add_search_only_titles_filter
    ', 10, 3);
    function add_search_only_titles_filter($args, $field, $post_id) {
      // only adds the search filter to this field
      add_filter('posts_search', '__search_by_title_only', 500, 2);
      return $args;
    }
    // this function copied from the source I linked above
    function __search_by_title_only( $search, &$wp_query )
    {
        global $wpdb;
    
        if ( empty( $search ) )
            return $search; // skip processing - no search term in query
    
        $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}) ";
            if ( ! is_user_logged_in() )
                $search .= " AND ($wpdb->posts.post_password = '') ";
        }
    
        return $search;
    }