Support

Account

Home Forums ACF PRO Searching ACF custom field data through WP REST API

Solved

Searching ACF custom field data through WP REST API

  • Hello,

    I’m building a theme using Vue and feeding data through the WP REST API. This is working fine for setting up and building out pages in a modular fashion using AFC Pro. I’m not registering the custom fields, I’m building them via PHP (https://www.advancedcustomfields.com/resources/register-fields-via-php/) and then feeding them through the rest API. The ACF data is attached to pages as that comes through as an “acf” object for page requests.

    However, I’m not sure how to access the contents of these custom fields through search via the WP REST API. I have a custom endpoint where I’m querying pages and posts, but ACF data doesn’t get searched, or exposed to search so it can be searched along pages and posts.

    Here’s something I’m doing to get search data through the REST API:

    
    /**
     * Register our custom route for search.
     */
    function global_register_search_route() {
      register_rest_route('/wp/v2/global', '/search', [
        'methods' => WP_REST_Server::READABLE,
        'callback' => 'global_ajax_search',
        'args' => global_get_search_args()
      ]);
    }
    add_action( 'rest_api_init', 'global_register_search_route');
    
    /**
     * Custom endpoint to retrieve pages, posts, and other custom post types.
     */
    function global_get_search_args() {
      $args = [];
      $args['s'] = [
        'description' => esc_html__( 'The search term.', 'global' ),
        'type'        => 'string',
      ];
    
      return $args;
    }
    
    /**
     * Use the request data to find the pages and posts we
     * are looking for and prepare them for use
     * on the front end.
     */
    function global_ajax_search( $request ) {
      $posts = [];
      $results = [];
      if ( isset($request['s'])) :
        $posts = get_posts([
          'posts_per_page' => get_option( 'posts_per_page' ),
          'post_type' => ['page', 'post'],
          's' => $request['s'],
        ]);
        foreach($posts as $post):
          $results[] = [
            'id'             => $post->ID,
            'type'           => get_post_type( $post->ID ),
            'title'          => $post->post_title,
            'link'           => get_permalink( $post->ID ),
            'author'         => $post->post_author,
            'content'        => $post->post_content,
            'excerpt'        => $post->post_excerpt,
            'featured-image' => get_the_post_thumbnail_url( $post->ID ),
            'categories'     => get_the_category( $post->ID ),
            'tags'           => get_the_tags( $post->ID ),
          ];
        endforeach;
      endif;
    
      if ( empty($results) ) :
        return new WP_Error( 'front_end_ajax_search', 'No results');
      endif;
    
      return rest_ensure_response( $results );
    }
    

    I’ve seen a lot of people talking about enabling search for custom fields, but the problem with that is they’re all dealing with exposing that data via default WordPress types of functions while I’m trying to access this data through the REST API.

    Any help or suggestions would be appreciated.

  • As a follow up, I spent some time trying to get this working. I created a custom query to expose ACF fields within a custom search endpoint. Searching keywords would only return results if it matched the title, or excerpt.

    I was finally able to get this working using a combo of SearchWP and the SearchWP API plugins.

    I’m not crazy about the fact that I had to use plugins to search custom fields and return results but it’s the only thing that worked.

  • Same issue here … are we going get any help from you guys on REST and ACF? This is 2020 already guys … what are you doing?

  • Search in WP only searches titles and content. However you use search this is what WP does. You should bring this up with WP. The only solutions are to use a plugin that modifies how WP works or to make the modifications yourself. There is an explanation of what’s needed here http://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/

  • If someone is looking for a solution to this problem is to recommend the plugin:
    https://wordpress.org/plugins/acf-better-search/

    This plugin adds to default WordPress search engine the ability to search by content from selected fields of Advanced Custom Fields plugin.

    Everything works automatically, no need to add any additional code.

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

You must be logged in to reply to this topic.