How to duplicate certain arbitrary positions?

There is a script that duplicates a post. But how to make that duplicated all the posts of a certain type (custom type posts) that were created a week ago?

// duplicate post
/*
 * The function creates post duplicate as a draft and redirects to his edit page
*/
true_duplicate_post_as_draft function(){
 global $wpdb;
 if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'true_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
 wp_die('Nothing to duplicate!');
}

/*
 * get the ID of the original post
*/
 $post_id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
/*
 * and then all his data
*/
 $post = get_post( $post_id );

/*
 * if you do not want the current author was the author of a new post
 * then replace the following two lines in: $new_post_author = $post->post_author;
 * when replacing these lines the author will be copied from the original post
*/
 $current_user = wp_get_current_user();
 $new_post_author = $current_user->ID;

/*
 * if the post exists, create a duplicate
*/
 if (isset( $post ) && $post != null) {

/*
 * array data for the new post
*/

 $new_post_date = date('Y-m-d H:i:s', time()- (7 * 24 * 60 * 60));
 $new_post_date_gmt = get_gmt_from_date($new_post_date);

 $args = array(
 'comment_status' => $post->comment_status,
 'ping_status' => $post->ping_status,
 'post_author' => $new_post_author,
 'post_content' => $post->post_content,
 'post_excerpt' => $post->post_excerpt,
 'post_name' => $post->post_name,
 'post_parent' => $post->post_parent,
 'post_password' => $post->post_password,
 'post_status' => 'publish', // draft, if you want to publish - replace on publish
 'post_title' => $post->post_title,
 'post_type' => $post->post_type,
 'to_ping' => $post->to_ping,
 'menu_order' => $post->menu_order,
 'post_date' => $new_post_date,
 'post_date_gmt' => $new_post_date_gmt,
 'post_modified' => $new_post_date,
 'post_modified_gmt' => $new_post_date_gmt,
);

/*
 * create the post with wp_insert_post()
*/
 $new_post_id = wp_insert_post( $args );

/*
 * assigned to a new post all the elements of the taxonomies (categories, tags etc) of the old
*/
 $taxonomies = get_object_taxonomies($post->post_type); // returns an array of the names of the taxonomies used for the post type, for example array("category", "post_tag");
 foreach ($taxonomies as $taxonomy) {
 $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
 wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}

/*
 * duplicate all the custom fields
*/
 $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
 if (count($post_meta_infos)!=0) {
 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
 foreach ($post_meta_infos as $meta_info) {
 $meta_key = $meta_info->meta_key;
 $meta_value = addslashes($meta_info->meta_value);
 $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
 $sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}


/*
 * finally, redirect the user to the edit page, new post
*/
 wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
 } else {
 wp_die('Error creating post, can't find the original post with ID=:' . $post_id);
}
}
add_action( 'admin_action_true_duplicate_post_as_draft', 'true_duplicate_post_as_draft' );


/*
 * Add a link to duplicate post for post_row_actions
*/
true_duplicate_post_link function( $actions, $post ) {
 if (current_user_can('edit_posts')) {
 $actions['duplicate'] = '<a href="admin.php?action=true_duplicate_post_as_draft&post=' . $post->ID . '" title="Duplicate this post" rel="permalink">Duplicate</a>';
}
 return $actions;
}

add_filter( 'post_row_actions', 'true_duplicate_post_link', 10, 2 );
July 12th 19 at 16:42
1 answer
July 12th 19 at 16:44
$posts = new WP_Query($array)

$posts contains all the necessary posts, pass on them with the cycle and run duplicate
but as to prescribe the conditions on a particular type of post and date of publication? - reuben commented on July 12th 19 at 16:47

Find more questions by tags WordPress