最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Auto delete post if certain custom field data is empty

matteradmin7PV0评论

This question can be stupid but I really need a solution for it.. I am generating classified post using cron job. I have some custom filed as well with the cron job like 'source_link'. Sometimes that custom filed data may not be set for error or any kind of situation. So I want that post with empty data for custom field 'source_link' to be deleted automatically. Is this possible anyway ?

Here the code how I do I create post and set custom field data

            kses_remove_filters();
            $new_id = wp_insert_post($post_arr, true);

            kses_init_filters();

            if (is_wp_error($new_id)) {
                $this->write_log("error occurred in wordpress post entry: " . $new_id->get_error_message() . " " . $new_id->get_error_code(), true);
                return;
            }
            update_post_meta($new_id, 'source_link', $url);

This question can be stupid but I really need a solution for it.. I am generating classified post using cron job. I have some custom filed as well with the cron job like 'source_link'. Sometimes that custom filed data may not be set for error or any kind of situation. So I want that post with empty data for custom field 'source_link' to be deleted automatically. Is this possible anyway ?

Here the code how I do I create post and set custom field data

            kses_remove_filters();
            $new_id = wp_insert_post($post_arr, true);

            kses_init_filters();

            if (is_wp_error($new_id)) {
                $this->write_log("error occurred in wordpress post entry: " . $new_id->get_error_message() . " " . $new_id->get_error_code(), true);
                return;
            }
            update_post_meta($new_id, 'source_link', $url);

Share Improve this question asked Mar 25, 2019 at 17:01 Tuhin A.Tuhin A. 1757 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1
    kses_remove_filters();
    $new_id = wp_insert_post($post_arr, true);

    kses_init_filters();

    if (is_wp_error($new_id)) {
        $this->write_log("error occurred in wordpress post entry: " . $new_id->get_error_message() . " " . $new_id->get_error_code(), true);
        return;
    }

    if($url)   update_post_meta($new_id, 'source_link', $url);
    else {
        // wp_delete_post($new_id); // Move post to trash
        $deleted = wp_delete_post($new_id, true); // Permanently delete post
        if(! $deleted ){
             $this->write_log("error deleting post entry.", true);
             return;
        }
    }
Post a comment

comment list (0)

  1. No comments so far