如何在发布帖子时获取自定义字段

时间:2018-04-20 作者:Paul John Pulumbarit

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);

    }
我需要从Wordpress后端将自定义字段获取到我的前端(Laravel)。该帖子已成功保存,我可以获取帖子ID和标题,但无法从自定义字段获取值。然后在I var\\u转储post数据之后。它显示:

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}]}}"
更新:第二个括号数据来自我的js中的th AJAX响应。因此get\\u posts()只返回第一个括号{},其中不包括acf字段值。

我的问题是,如何从第二个括号中获取数据?

顺便说一下,我正在使用ACF到REST API,以便将ACF数据获取到我的Laravel前端。

1 个回复
SO网友:Dharmishtha Patel
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;
    }
}
结束

相关推荐

Php致命错误:无法将WP_REST_RESPONSE类型的对象用作wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php中

我向WordPress添加了一个自定义端点,如下所示: add_action( \'rest_api_init\', function () { register_rest_route( \'menc/v1\', \'/crosscat/(?P[\\w-]+)/(?P[\\w-]+)\', array( \'methods\' => \'GET\', \'callback\' => \'dept_cat_api\',&#x

如何在发布帖子时获取自定义字段 - 小码农CODE - 行之有效找到问题解决它

如何在发布帖子时获取自定义字段

时间:2018-04-20 作者:Paul John Pulumbarit

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);

    }
我需要从Wordpress后端将自定义字段获取到我的前端(Laravel)。该帖子已成功保存,我可以获取帖子ID和标题,但无法从自定义字段获取值。然后在I var\\u转储post数据之后。它显示:

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}]}}"
更新:第二个括号数据来自我的js中的th AJAX响应。因此get\\u posts()只返回第一个括号{},其中不包括acf字段值。

我的问题是,如何从第二个括号中获取数据?

顺便说一下,我正在使用ACF到REST API,以便将ACF数据获取到我的Laravel前端。

1 个回复
SO网友:Dharmishtha Patel
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;
    }
}