Support

Account

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

Solving

Create json api from ACF plugin data.

    • boka003

    • February 26, 2021 at 8:25 am

    Hello there,

    We have using your plugin in our sidebar to show recommended posts.

    So we create one simple widget where authors inside post editor can select is post recommended.

    And it is all working very well.

    —- My question is, how we can create wp json api from posts that are selected (showing in sidebar)?

    At our web site, everything is working fine, but one friend of mine is trying to create mobile application for our web site. So we need to send him json api for recommended posts.

    Is there any easy way to create that api ?
    I am not a developer.

    Thank you very mush!

    Best Regards

    • boka003

    • February 28, 2021 at 7:40 am

    Hi there,

    I found this amazing plugin: https://wordpress.org/plugins/acf-to-rest-api/

    Now when i open json at /wp-json/wp/v2/posts i can see filed ACF

    And there i can see fileds recommended_sidebar: yes or recommended_sidebar: no

    But this JSON will show all posts.
    Is it possible to make some filter for posts?
    I will like to show only posts that have recommended_sidebar: yes ?

    Thank you!

    • boka003

    • March 1, 2021 at 11:45 pm

    BUMP 🙁

  • @boka003 creating an API to allow showing posts with specific field values is beyond the scope of help the you’re probably going to find on this forum.

    The limit of my knowledge is that you’re going to need to create a custom end point for the WP REST API. When this custom end point is called a query needs to be done to get and return the posts that you want to be shown.

    https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
    https://www.google.com/search?client=firefox-b-1-d&q=wp+json+api+create+custom+end+point

    • boka003

    • March 2, 2021 at 10:40 pm

    Thank you @hube2

    So i will need to create custom endpoint ?
    It is not possible to just filter endpoint that i allredy have?

    Regards

  • You will need to do a custom query to get only posts with a custom field that has a specific value. I do not know enough about the rest api to know if you can filter the posts that are already being returned in the current end point or you need to create a custom end point.

  • Hello,

    I have do a custom query.

    But for some reason, only sticky posts will show up in endpoint. Not a custom filed posts.

    I post my query here:
    https://wordpress.stackexchange.com/questions/384526/json-create-rest-api-endpoint-for-advanced-custom-fields

    I hope there is something that i can do to fix my query.

    Best Regards

    • boka003

    • March 7, 2021 at 11:33 pm

    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;
    } 
  • Update here, endpoint is showing only posts that have status sticky + custom files: yes

    If post is only sticky, it will not show
    If post have only custom filed: yes, it will not show.

    Post will only show if have sticky + custom filed: yes

Viewing 9 posts - 1 through 9 (of 9 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.