1
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 – 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.

7
  • If you have the ACF data inside the REST API so you can parse this JSON from outside WordPress in your Laravel front end to get this values.
    – bueltge
    Commented Apr 20, 2018 at 8:16
  • I just checked that the JSON (the second bracket) came from the responseText of the ajax. My problem is how can I get the acf fields values of new published post(order in my case)? I can get the Post ID, title and etc except the acf fields values by just using $post->ID Commented Apr 20, 2018 at 8:19
  • ACF fields are custom meta fields to a post, so that you can use get_post_meta() to get the values to post-ID and his field key. However you write that you add the ACF content to the REST API, so that you can parse the JSON of the REST API to get also the meta data to each post.
    – bueltge
    Commented Apr 20, 2018 at 8:23
  • I used WP Rest API (wp-json) and added a plugin so that I can get the acf fields in my Frontend(Laravel). I can also create a post with acf fields using my front end and wordpress as my back end. The only problem is I need to send the values of acf fields in the email when the order is published. The email was sent but the values from ACF Fields are blank. I already used get_post_meta($postID, field key of the acf field) but still no luck. But when I send an email when I update a post through backend, $_POST['acf']['field_key'] works. Commented Apr 20, 2018 at 8:28
  • The way is correct to get meta ta to a post. But if you use ACF maybe you should use the API rom ACF, like get_field() or get_fields()
    – bueltge
    Commented Apr 20, 2018 at 8:31

1 Answer 1

0
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;
    }
}
1
  • I am sorry, but I need to get the submitted order data from my front end (laravel). I used $.ajax to send POST to Wordpress. I can get the ID, title etc of the post just the acf fields. Look at the data on my post. The Post object divides 2 data using {}. I was able to get the data inside the first bracket. I think the 2nd bracket is a JSON data from ACF Rest API plugin. Commented Apr 20, 2018 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.