admin管理员组文章数量:1122832
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\/{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.
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.
Share Improve this question edited Apr 20, 2018 at 8:12 Paul John Pulumbarit asked Apr 20, 2018 at 6:13 Paul John PulumbaritPaul John Pulumbarit 115 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 0add_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;
}
}
本文标签: rest apiHow to get custom fields in a post when published
版权声明:本文标题:rest api - How to get custom fields in a post when published 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283871a1927111.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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:23get_field()
orget_fields()
– bueltge Commented Apr 20, 2018 at 8:31