-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy-move-posts-function.php
More file actions
126 lines (116 loc) · 3.9 KB
/
copy-move-posts-function.php
File metadata and controls
126 lines (116 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/*
Plugin Name: Copy Move posts
Author: Ujjaval Jani
Version: 1.6
Description: You can Copy or Move posts from one Post type to another Post type with custom field,thumbnail and meta information.
License: GPLv3
*/
ini_set('max_execution_time', 0);
function CPMV_my_admin_menu() {
add_menu_page( 'Copy Move Posts', 'Move or Copy', 'manage_options', 'copy-move-posts/copy-move-posts-admin-page.php', '', 'dashicons-tickets','61' );
}
add_action( 'admin_menu', 'CPMV_my_admin_menu' );
function CPMV_get_posts_types()
{
$get_cpt_args = array(
'public' => true,
);
$post_types=get_post_types($get_cpt_args,'object');
unset($post_types['attachment']);
return $post_types;
}
add_filter('CPMV_get_posts_types','CPMV_get_posts_types');
function CPMV_convert_post()
{
if(isset($_POST['change_post']) && filter_var($_POST['change_post'], FILTER_SANITIZE_STRING)=='Complete')
{ unset($_POST['change_post']);
if(isset($_POST['need_count']))
{
if($_POST['need_count']=='' || $_POST['need_count']<'-1')
{
$pagination=-1;
}
else
{
$pagination=$_POST['need_count'];
}
}
else
{
$pagination=-1;
}
if(filter_var($_POST['cmp'], FILTER_SANITIZE_STRING)==1) ///Copy posts
{
global $wpdb;
$args = array('post_type'=> filter_var($_POST['post_from'],FILTER_SANITIZE_STRING),'posts_per_page'=> filter_var($pagination,FILTER_SANITIZE_STRING));
$the_query = new WP_Query( $args );
foreach ($the_query->posts as $posts) {
$post_meta=get_post_meta( $posts->ID, $key = '', $single = false );
unset($post_meta['_edit_lock']);
unset($post_meta['_edit_last']);
//exit();
//print_r($post_meta);
//$attach_id=$post_meta['_thumbnail_id'];
//print_r($post_meta);
//echo get_bloginfo('version');
//add_post_meta($post_id,'Archived','testsst');
if(get_bloginfo('version')>='4.4.0')
{
$meta_info=array();
foreach ($post_meta as $key => $value) {
//echo $key.'='.$value[0].',';
$meta_info[$key]=$value[0];
}
$my_post = array(
'post_title' => $posts->post_title,
'post_content' => $posts->post_content,
'post_excerpt' => $posts->post_excerpt,
'post_status' => $posts->post_status,
'post_author' => get_current_user_id(),
'comment_status' => $posts->comment_status,
'post_type' => filter_var($_POST['post_to'],FILTER_SANITIZE_STRING),
'meta_input' =>$meta_info,
);
//print_r($my_post);
//exit();
$post_id=wp_insert_post( $my_post , true);
}
else
{
$my_post = array(
'post_title' => $posts->post_title,
'post_content' => $posts->post_content,
'post_excerpt' => $posts->post_excerpt,
'post_status' => $posts->post_status,
'post_author' => get_current_user_id(),
'comment_status' => $posts->comment_status,
'post_type' => filter_var($_POST['post_to'],FILTER_SANITIZE_STRING),
);
$post_id=wp_insert_post( $my_post , true);
foreach ($post_meta as $key => $value) {
add_post_meta($post_id,$key,$value[0]);
}
}
}
}
elseif(filter_var($_POST['cmp'], FILTER_SANITIZE_STRING)==2) ///Move posts
{
global $wpdb;
$args = array('post_type'=> filter_var($_POST['post_from'],FILTER_SANITIZE_STRING),'posts_per_page'=> filter_var($pagination,FILTER_SANITIZE_STRING));
$the_query = new WP_Query( $args );
//echo "<pre>";
//print_r($the_query);
foreach ($the_query->posts as $posts)
{
//print_r($posts);
$my_post = array(
'ID' => $posts->ID,
'post_type' => filter_var($_POST['post_to'],FILTER_SANITIZE_STRING),
);
$post_id=wp_update_post( $my_post , true);
}
}
}
}
add_action('init', 'CPMV_convert_post');