Support

Account

Home Forums General Issues Create json api from ACF plugin data. Reply To: Create json api from ACF plugin data.

  • I have write in many wordpress groups, but no one know how i can solve this.
    here is my updated code:

    
    add_action( 'rest_api_init', 'api_hooks' );
    function api_hooks() {
    
        register_rest_route( 'get-post-sidebar/v1', '/go', array(
            'methods'  => 'GET',
            'callback' => 'get_post_sidebar',
        ) );
    
    }
    function get_post_sidebar($request_data){
      // $data = $request_data->get_params();
    
      $data = array()
    ;
    
      $args = array(
      'post_type'   => 'post',
      'post_status' => 'publish',
      'orderby'   => 'id',
      'order'     => 'DESC',
      'meta_key'     => 'recommended_sidebar',
      'meta_value'    => 'yes', 
      'compare'   => '=',
    
      );
    
      $the_query = new WP_Query( $args );
    
      while ( $the_query->have_posts() ) {
          $the_query->the_post();
          array_push($data,
              array(
                  'title' => get_the_title(),
                  'content' => get_the_content(),
                  'date' => get_the_date('Y-m-d H:i'),
                  'number_of_comments' => get_comments_number(),
                  'author' => get_the_author(),
                  'id' => get_the_ID(),
                  'link' => get_post_permalink(),
                  'thumbnail' => get_the_post_thumbnail_url()
              )
          );
      }
    
      wp_reset_postdata();
    
      $response = new \WP_REST_Response( $data );
      $response->set_status( 200 );
    
      return $response;
    }