0

I am trying to create, update & delete posts programmatically from an API endpoint inside my plugin. My idea was to display these posts later on a dedicated page.

Here's what I've written so far:

function get_hardware_posts(){
    if(false === ( $hardware_api_response = get_transient( 'hardware_api_response' ) )) {
        $hardware_api_response = json_decode(wp_remote_get(api)['body']);
        set_transient('hardware_api_response', $hardware_api_response, DAY_IN_SECONDS);
    }
    $hardware_api_response = get_transient('hardware_api_response');

        
    foreach($hardware_api_response->data as $hardware_list){
        foreach($hardware_list->models as $model){
            $post_exists = (new WP_Query(['post_type' => 'hardware', 'name' => $model->debug->external_payload->slug]))->found_posts > 0;
            $post_template = array(
                'post_title' => $model->name,
                'post_name' => $model->debug->external_payload->slug,
                'post_author' => 1,
                'post_type' => 'hardware',
                'post_status' => 'publish',
                'meta_input' => array(
                    'name' => $model->name,
                    'image_url' => !empty($model->image_url) ? $model->image_url : $hardware_list->image_url,
                    'features' => !empty($model->debug->external_payload->features) ? $model->debug->external_payload->features : '',
                    'config_meter_reading' => !empty($model->config_meter_reading) ? $model->config_meter_reading : '',
                    'config_default_max_kw' => !empty($model->config_default_max_kw) ? $model->config_default_max_kw : '',
                    'current_type' => !empty($model->current_type) ? $model->current_type : '',
                    'popularity_score' => !empty($model->popularity_score) ? $model->popularity_score : '',
                    'stability_score' => !empty($model->stability_score) ? $model->stability_score : '',
                    'rating_score' => !empty($model->rating_score) ? $model->rating_score : '',
                    'total_scoore' => !empty($model->total_score) ? $model->total_score : '',
                    'rank' => !empty($model->rank) ? $model->rank : '',
                    'meter_class' => !empty($model->debug->external_payload->meter_class) ? $model->debug->external_payload->meter_class : '',
                    'rfid_reader' => !empty($model->debug->external_payload->rfid_reader) ? $model->debug->external_payload->rfid_reader : '',
                    'integrations' => !empty($model->debug->external_payload->integrations) ? $model->debug->external_payload->integrations : '',
                    'de_conformity' => !empty($model->debug->external_payload->de_conformity) ? $model->debug->external_payload->de_conformity : '',
                    'identity_type' => !empty($model->debug->external_payload->identity_type) ? $model->debug->external_payload->identity_type : '',
                    'supported_kwh' => !empty($model->debug->external_payload->supported_kwh) ? $model->debug->external_payload->supported_kwh : '',
                    'connector_count' => !empty($model->debug->external_payload->connector_count) ? $model->debug->external_payload->connector_count : '',
                    'connector_types' => !empty($model->debug->external_payload->connector_types) ? $model->debug->external_payload->connector_types : '',
                    'tax_refund_approved' => !empty($model->debug->external_payload->tax_refund_approved) ? $model->debug->external_payload->tax_refund_approved : '',
                    "ocpp_configuration_mode" => !empty($model->debug->external_payload->ocpp_configuration_mode) ? $model->debug->external_payload->ocpp_configuration_mode : '',
                )
            );
            if($post_exists) {
                $post_id = (new WP_Query(['post_type' => 'hardware', 'name' => $model->debug->external_payload->slug]))->posts[0]->ID;
                array_unshift($post_template, $post_id);
                $output_id = wp_update_post($post_template);
            }else {
                wp_insert_post($post_template);
            }
        }
    }
}
do_action(hook, get_hardware_posts());

Because I am new to plugin development I don't know which hook to use; what hook would you recommend to use when dealing with my approach?

1 Answer 1

0

Hooks are not for deleting or creating posts, they are kind of event listeners. For example 'delete_post': "Fires immediately before a post is deleted from the database." [https://developer.wordpress.org/reference/hooks/delete_post/]

You need to use the WordPress REST API: https://developer.wordpress.org/rest-api/reference/posts/

This article by Misha Rudrastyh gives a good intro: https://rudrastyh.com/wordpress/rest-api-create-delete-posts.html

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.