Support

Account

Home Forums ACF PRO Displaying layout fields retrieved via REST API

Solved

Displaying layout fields retrieved via REST API

  • I’m working on pulling blog posts from 1 site into another using the REST API, all works fine for pulling in the basics: title, excerpt and featured image, I’ve used a tutorial as a base and my code in php is below. My question is how to pull in ACF fields and display in the posts loop below? The acf fields are exposed and I can see them in the endpoint, I’m just not sure how to pull them in. I’ve put a sample of the json below also…

    PHP TO DISPLAY

    function get_posts_via_rest() {
    // Initialize variable.
    $allposts = ”;

    // Enter the name of your blog here followed by /wp-json/wp/v2/posts and add filters like this one that limits the result to 2 posts.
    $response = wp_remote_get( ‘https://www.sumydesigns.com/wp-json/wp/v2/posts?per_page=2’ );
    // Exit if error.
    if ( is_wp_error( $response ) ) {
    return;
    }
    // Get the body.
    $posts = json_decode( wp_remote_retrieve_body( $response ) );
    // Exit if nothing is returned.
    if ( empty( $posts ) ) {
    return;
    }
    // If there are posts.
    if ( ! empty( $posts ) ) {
    // For each post.
    foreach ( $posts as $post ) {
    // Use print_r($post); to get the details of the post and all available fields
    // Format the date.
    $fordate = date( ‘n/j/Y’, strtotime( $post->modified ) );
    // Show a linked title and post date.
    $allposts .= ‘link ) . ‘” target=\”_blank\”>’ . esc_html( $post->title->rendered ) . ‘ ‘ . esc_html( $fordate ) . ‘<br />’;
    }

    return $allposts;
    }
    }
    // Register as a shortcode to be used on the site.
    add_shortcode( ‘sc_get_posts_via_rest’, ‘get_posts_via_rest’ );

    JSON:

    {
    id: 38096,
    date: “2019-11-25T09:56:14”,
    date_gmt: “2019-11-25T09:56:14”,
    guid: {
    rendered: “link removed”
    },
    modified: “2019-11-25T09:56:14”,
    modified_gmt: “2019-11-25T09:56:14”,
    slug: “link-removed”,
    status: “publish”,
    type: “post”,
    link: “link-removed”,
    title: {
    rendered: “link-removed”
    },
    content: {
    rendered: “”,
    protected: false
    },
    excerpt: {
    rendered: “<p>Example excerpt content</p> “,
    protected: false
    },
    author: 114,
    featured_media: 38100,
    comment_status: “open”,
    ping_status: “open”,
    sticky: false,
    template: “”,
    format: “standard”,
    meta: [ ],
    categories: [
    12,
    40
    ],
    tags: [
    20,
    8
    ],
    acf: {
    acf_field_search_weight_override: “”,
    acf_field_region_selection: “all”,
    acf_field_stripped_down_options: “default”,
    acf_option_off_featured_image_hero: true,
    acf_option_donate_override: false,
    acf_field_page_content: [
    {
    acf_fc_layout: “acf_layout_full_screen_media”,
    acf_field_full_screen_media_content_type: “image”,
    acf_field_full_screen_media_image: {},
    acf_field_full_screen_media_video: “”,
    acf_field_full_screen_media_optional_thumbnail: false,
    acf_field_full_screen_media_gallery: false,
    acf_field_full_screen_media_show_image_caption: true,
    acf_field_full_screen_media_image_caption_position: “left”,
    acf_field_full_screen_media_show_gallery_captions: false,
    acf_field_remove_margin_bottom: false,
    enable_region_selection: false,
    acf_field_module_region_selection: “all”
    },
    {},
    {
    acf_fc_layout: “acf_layout_share_page”,
    acf_field_share_page_social_

  • For anyone looking to get acf fields from rest api using php:

    $fullcontent = $post->{‘acf’}->{‘acf_field_page_content’}[1]->{‘acf_field_text_block’};

    🙂

  • Hi @roweenaweb

    is it still so hard to get them? Maybe there is a way via the ACF API like get_field() ?
    https://www.advancedcustomfields.com/resources/get_field/

    Br, John

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

You must be logged in to reply to this topic.