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

    • resaip

    • October 9, 2019 at 7:10 am

    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.

    • resaip

    • October 11, 2019 at 3:36 am

    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.

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.