How can I automatically add random description to posts with empty content posted to WordPress?

Tags: , ,



I would like to automate the process of adding descriptions for articles published on my site. I would like to automatically insert a random description from a list of pre-edited (Advanced Custom Fields) descriptions when a new post is published as draft. eg: “description_1” or “description_2” or “description_100” (randomly)

I have this code, which seems to do the trick, but I still have to insert the parameter to randomize the choice of description (randomly choose a number between 1 and 100 just after the word “description”) on the line: // Custom post content $content = . $description_1 . ; of this code above. I would also like to add these two little conditions:

  • The content of the article is empty
  • The status of the article is set to “draft” Thanks for any response!
function show_update_postdata( $value, $post_id, $field ) {

        // Get values from POST
        $description_1 = $_POST['acf']['field_5dd00e0582125'];
        $description_2 = $_POST['acf']['field_5dd00e0582125'];
        $description_50 = $_POST['acf']['field_5dd00e0582125'];
        $description_100 = $_POST['acf']['field_5dd00e0582125'];

        // Custom post content
        $content = . $description_1 . ;
        $postdata = array(
             'ID'          => $post_id,
             'post_content'  => $content,
             'post_type'   => 'post',
        );

        wp_update_post( $postdata );
        return $value;

}
add_filter('acf/update_value/name=description_1', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=description_2', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=description_50', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=description_100', 'show_update_postdata', 10, 3);``` 

Answer

You can use two fucntion fisrt for content get_post_field() and the second is for check post status get_post_status(). check below code.

function show_update_postdata( $value, $post_id, $field ) {

    $content = apply_filters( 'the_content', get_post_field( 'post_content', $post_id ) );

    if( $content == '' && get_post_status( $post_id ) == 'draft' ){

        // Get values from POST
        $description_1 = $_POST['acf']['field_5dd00e0582125'];
        $description_2 = $_POST['acf']['field_5dd00e0582125'];
        $description_50 = $_POST['acf']['field_5dd00e0582125'];
        $description_100 = $_POST['acf']['field_5dd00e0582125'];

        $random_content = array( $description_1, $description_2, $description_50, $description_100 );
        $random_content = $random_content[ array_rand( $random_content ) ];

        $postdata = array(
             'ID'            => $post_id,
             'post_content'  => $random_content,
             'post_type'     => 'post',
        );

        wp_update_post( $postdata );
    }
    
    return $value;


}
add_filter('acf/update_value/name=description_1', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=description_2', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=description_50', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=description_100', 'show_update_postdata', 10, 3);


Source: stackoverflow