Thursday, September 8, 2022
HomeWordPress Developmentplugins - Appropriate hook when creating, updating and deleting posts programmatically

plugins – Appropriate hook when creating, updating and deleting posts programmatically


I’m attempting to create, replace & delete posts programmatically from an API endpoint inside my plugin. My concept was to show these posts in a while a devoted web page.

This is what I’ve written up to now:

operate 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->knowledge as $hardware_list){
        foreach($hardware_list->fashions as $mannequin){
            $post_exists = (new WP_Query(['post_type' => 'hardware', 'name' => $model->debug->external_payload->slug]))->found_posts > 0;
            $post_template = array(
                'post_title' => $model->title,
                'post_name' => $model->debug->external_payload->slug,
                'post_author' => 1,
                'post_type' => '{hardware}',
                'post_status' => 'publish',
                'meta_input' => array(
                    'title' => $model->title,
                    'image_url' => !empty($model->image_url) ? $model->image_url : $hardware_list->image_url,
                    'options' => !empty($model->debug->external_payload->options) ? $model->debug->external_payload->options : '',
                    '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());

As a result of I’m new to plugin improvement I do not know which hook to make use of; what hook would you advocate to make use of when coping with my method?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments