How to get custom fields in a post when published

add_action( 'publish_post', 'sm12_publish_post' );
    add_action( 'save_post', 'sm12_publish_post', 20 ); //After save meta value
    function sm12_publish_post( $postid ){

        /** some checks */

        if( "publish" != get_post_status( $postid ) )
            return;

        $post= get_post($postid);
        $sub = 'test subject';
        $mgs = 'test message';

        wp_mail( '[email protected]', $email, $mgs);

    }

I need to get the custom fields to my frontend (Laravel) from Wordpress Backend. The post was successfully saved, and I was able to get the Post ID and title, but I can't fetch the values from custom field. Then after I var_dump the post data. It shows:

object(WP_Post)#1791 (24) {
      ["ID"]=
      int(789)
      ["post_author"]=
      string(1) "1"
      ["post_date"]=
      string(19) "2018-04-20 06:06:32"
      ["post_date_gmt"]=
      string(19) "2018-04-20 06:06:32"
      ["post_content"]=
      string(0) ""
      ["post_title"]=
      string(33) "Laundry Service - Quintessa Walls"
      ["post_excerpt"]=
      string(0) ""
      ["post_status"]=
      string(7) "publish"
      ["comment_status"]=
      string(6) "closed"
      ["ping_status"]=
      string(6) "closed"
      ["post_password"]=
      string(0) ""
      ["post_name"]=
      string(31) "laundry-service-quintessa-walls"
      ["to_ping"]=
      string(0) ""
      ["pinged"]=
      string(0) ""
      ["post_modified"]=
      string(19) "2018-04-20 06:06:32"
      ["post_modified_gmt"]=
      string(19) "2018-04-20 06:06:32"
      ["post_content_filtered"]=
      string(0) ""
      ["post_parent"]=
      int(0)
      ["guid"]=
      string(65) "http://localhost/smartend/orders/laundry-service-quintessa-walls/"
      ["menu_order"]=
      int(0)
      ["post_type"]=
      string(6) "orders"
      ["post_mime_type"]=
      string(0) ""
      ["comment_count"]=
      string(1) "0"
      ["filter"]=
      string(3) "raw"
    }
    {"id":789,"date":"2018-04-20T06:06:32","date_gmt":"2018-04-20T06:06:32","guid":{"rendered":"http:\/\/localhost\/smartend\/orders\/laundry-service-quintessa-walls\/","raw":"http:\/\/localhost\/smartend\/orders\/laundry-service-quintessa-walls\/"},"modified":"2018-04-20T06:06:32","modified_gmt":"2018-04-20T06:06:32","password":"","slug":"laundry-service-quintessa-walls","status":"publish","type":"orders","link":"http:\/\/localhost\/smartend\/orders\/laundry-service-quintessa-walls\/","title":{"raw":"Laundry Service - Quintessa Walls","rendered":"Laundry Service #8211; Quintessa Walls"},"template":"","acf":{"customer_name":"Quintessa Walls","service_name":"Laundry Service","customer_phone_number":"+793-64-2761068","customer_email":"[email protected]","order_quantity":"501","order_date":"04\/20\/2018","order_notes":"Eum ab dolor ipsa non consequatur Repellendus Elit ratione ad ad totam qui ut vel culpa","order_status":"Pending"},"_links":{"self":[{"href":"http:\/\/localhost\/smartend\/wp-json\/wp\/v2\/orders\/789"}],"collection":[{"href":"http:\/\/localhost\/smartend\/wp-json\/wp\/v2\/orders"}],"about":[{"href":"http:\/\/localhost\/smartend\/wp-json\/wp\/v2\/types\/orders"}],"version-history":[{"href":"http:\/\/localhost\/smartend\/wp-json\/wp\/v2\/orders\/789\/revisions"}],"wp:attachment":[{"href":"http:\/\/localhost\/smartend\/wp-json\/wp\/v2\/media?parent=789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}"

UPDATE: The second bracket data, came from the response of th AJAX in my js. So get_posts() only return the first bracket {} which not includes the acf fields values.

My question is, how to get the data from the second brackets?

By the way, I'm using ACF to REST API so that I can get the acf data to my Laravel front end.

Topic advanced-custom-fields rest-api Wordpress

Category Web


add_action('publish_post','create_custom_field')

function create_custom_field ($post_id) {
    global $wpdb;
    if ( current_user_can('contributor') ) {
        if(get_post_meta($post_id,'custom_field_1',true) == '1'){
            add_post_meta($post_ID,'submit','1',true);
            return $post_id;
        }

        return $post_id;
    }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.