WordPress

rest api — Jak uzyskać niestandardowe pola w poście po opublikowaniu

  • 20 kwietnia, 2018
  • 3 min read
rest api — Jak uzyskać niestandardowe pola w poście po opublikowaniu


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( 'test@gmail.com', $email, $mgs);

    }

Muszę pobrać niestandardowe pola do mojego frontendu (Laravel) z WordPress Backend. Post został pomyślnie zapisany i udało mi się uzyskać identyfikator posta i tytuł, ale nie mogę pobrać wartości z niestandardowego pola. Następnie po tym, jak var_dump zrzucę dane postu. To pokazuje:

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) "
      ["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":"vyniqemo@mailinator.com","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}]}}"

AKTUALIZACJA: Drugie dane nawiasów pochodziły z odpowiedzi th AJAX w moim js. Więc get_posts() zwraca tylko pierwszy nawias {}, który nie zawiera wartości pól acf.

Warto przeczytać!  „Object-cache.php” pojawia się ponownie po jego usunięciu, wyłącza wp_cron, a nawet wyłącza całą moją witrynę

Moje pytanie brzmi: jak uzyskać dane z drugiego nawiasu?

Nawiasem mówiąc, używam interfejsu API ACF do REST, dzięki czemu mogę uzyskać dane acf do mojego frontonu Laravel.


Źródło