{"id":5085,"date":"2018-02-28T14:07:19","date_gmt":"2018-02-28T19:07:19","guid":{"rendered":"https:\/\/themespress.ca\/?p=5085"},"modified":"2022-10-01T14:40:31","modified_gmt":"2022-10-01T18:40:31","slug":"how-to-duplicate-articles-without-plugins","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/how-to-duplicate-articles-without-plugins\/","title":{"rendered":"How to Duplicate Articles Without Plugins"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>With this tutorial, we will use a function that will allow you to duplicate a WordPress post without using a plugin. Admins can easily duplicate a draft to save time.<\/p>\n<p>It is important to understand that while duplicating is a time-saver, we have to be careful with the next post. We need to double-check the URL&#8217;s slug in the duplicated post because it will have the same URL with a -2 appended to it meaning you&#8217;ll need to tailor it based on the newest post.<\/p>\n<p>In our function if we do an <strong>isset get post<\/strong>, we&#8217;ll first validate if the user has admin rights. And then we fetch the previous post&#8217;s attributes and afterwards we use <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_insert_post\/\" target=\"_blank\" rel=\"noopener\">wp_insert_post<\/a> in order to recuperate taxonomy and category information. Finally, we validate and fetch all of the posts&#8217; meta and add them into an SQL query. <\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\"><div id=\"ts-enlighterjs-container-2398931\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-2398931\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">function themepress_duplicate_post_as_draft(){\n\tglobal $wpdb;\n\tif (! ( isset( $_GET[&#039;post&#039;]) || isset( $_POST[&#039;post&#039;])  || ( isset($_REQUEST[&#039;action&#039;]) &amp;&amp; &#039;themepress_duplicate_post_as_draft&#039; == $_REQUEST[&#039;action&#039;] ) ) ) {\n\twp_die(&#039;No post to duplicate has been supplied!&#039;);\n\t}\n \n\t\/*\n\t * get the original post id\n\t *\/\n\t$post_id = (isset($_GET[&#039;post&#039;]) ? $_GET[&#039;post&#039;] : $_POST[&#039;post&#039;]);\n\t\/*\n\t * and all the original post data then\n\t *\/\n\t$post = get_post( $post_id );\n \n\t\/*\n\t * if you don&#039;t want current user to be the new post author,\n\t * then change next couple of lines to this: $new_post_author = $post-&gt;post_author;\n\t *\/\n\t$current_user = wp_get_current_user();\n\t$new_post_author = $current_user-&gt;ID;\n \n\t\/*\n\t * if post data exists, create the post duplicate\n\t *\/\n\t if (isset( $post ) &amp;&amp; $post != null) {\n \n\t\t\/*\n\t\t * new post data array\n\t\t *\/\n\t\t$args = array(\n\t\t\t&#039;comment_status&#039; =&gt; $post-&gt;comment_status,\n\t\t\t&#039;ping_status&#039;    =&gt; $post-&gt;ping_status,\n\t\t\t&#039;post_author&#039;    =&gt; $new_post_author,\n\t\t\t&#039;post_content&#039;   =&gt; $post-&gt;post_content,\n\t\t\t&#039;post_excerpt&#039;   =&gt; $post-&gt;post_excerpt,\n\t\t\t&#039;post_name&#039;      =&gt; $post-&gt;post_name,\n\t\t\t&#039;post_parent&#039;    =&gt; $post-&gt;post_parent,\n\t\t\t&#039;post_passwothemepress&#039;  =&gt; $post-&gt;post_passwothemepress,\n\t\t\t&#039;post_status&#039;    =&gt; &#039;draft&#039;,\n\t\t\t&#039;post_title&#039;     =&gt; $post-&gt;post_title,\n\t\t\t&#039;post_type&#039;      =&gt; $post-&gt;post_type,\n\t\t\t&#039;to_ping&#039;        =&gt; $post-&gt;to_ping,\n\t\t\t&#039;menu_othemepresser&#039;     =&gt; $post-&gt;menu_othemepresser\n\t\t);\n \n\t\t\/*\n\t\t * insert the post by wp_insert_post() function\n\t\t *\/\n\t\t$new_post_id = wp_insert_post( $args );\n \n\t\t\/*\n\t\t * get all current post terms ad set them to the new post draft\n\t\t *\/\n\t\t$taxonomies = get_object_taxonomies($post-&gt;post_type); \/\/ returns array of taxonomy names for post type, ex array(&quot;category&quot;, &quot;post_tag&quot;);\n\t\tforeach ($taxonomies as $taxonomy) {\n\t\t\t$post_terms = wp_get_object_terms($post_id, $taxonomy, array(&#039;fields&#039; =&gt; &#039;slugs&#039;));\n\t\t\twp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);\n\t\t}\n \n\t\t\/*\n\t\t * duplicate all post meta just in two SQL queries\n\t\t *\/\n\t\t$post_meta_infos = $wpdb-&gt;get_results(&quot;SELECT meta_key, meta_value FROM $wpdb-&gt;postmeta WHERE post_id=$post_id&quot;);\n\t\tif (count($post_meta_infos)!=0) {\n\t\t\t$sql_query = &quot;INSERT INTO $wpdb-&gt;postmeta (post_id, meta_key, meta_value) &quot;;\n\t\t\tforeach ($post_meta_infos as $meta_info) {\n\t\t\t\t$meta_key = $meta_info-&gt;meta_key;\n\t\t\t\t$meta_value = addslashes($meta_info-&gt;meta_value);\n\t\t\t\t$sql_query_sel[]= &quot;SELECT $new_post_id, &#039;$meta_key&#039;, &#039;$meta_value&#039;&quot;;\n\t\t\t}\n\t\t\t$sql_query.= implode(&quot; UNION ALL &quot;, $sql_query_sel);\n\t\t\t$wpdb-&gt;query($sql_query);\n\t\t}\n\t\t\/*\n\t\t * finally, redirect to the edit post screen for the new draft\n\t\t *\/\n\t\twp_redirect( admin_url( &#039;post.php?action=edit&amp;amp;post=&#039; . $new_post_id ) );\n\t\texit;\n\t} else {\n\t\twp_die(&#039;Post creation failed, could not find original post: &#039; . $post_id);\n\t}\n}\nadd_action( &#039;admin_action_themepress_duplicate_post_as_draft&#039;, &#039;themepress_duplicate_post_as_draft&#039; );\n \n\/*\n * Add the duplicate link to action list for post_row_actions\n *\/\nfunction themepress_duplicate_post_link( $actions, $post ) {\n\tif (current_user_can(&#039;edit_posts&#039;)) {\n\t\t$actions[&#039;duplicate&#039;] = &#039;&lt;a title=&quot;Dupliquer&quot; href=&quot;admin.php?action=themepress_duplicate_post_as_draft&amp;amp;post=&#039; . $post-&gt;ID . &#039;&quot; rel=&quot;permalink&quot;&gt;Duplicate&lt;\/a&gt;&#039;;\n\t}\n\treturn $actions;\n}\n \nadd_filter( &#039;post_row_actions&#039;, &#039;themepress_duplicate_post_link&#039;, 10, 2 );\nadd_filter( &#039;page_row_actions&#039;, &#039;themepress_duplicate_post_link&#039;, 10, 2 );<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>We noticed that once the requirements have been filled, we enable the filter where we need it. In this case, we have two filters: <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/post_row_actions\/\" target=\"_blank\" rel=\"noopener\">post_row_actions<\/a> and <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/page_row_actions-6\/\" target=\"_blank\" rel=\"noopener\">page_row_actions<\/a>. One for the post and the other for the pages meaning that duplication will be enabled for both types.<br \/>\nIf you want to do it for another post type such as Portfolio or testimonial, you&#8217;ll a filter similar to this: add_filter( \u2018<strong>nomdutypedepost<\/strong>_row_actions\u2019, \u2018themepress_duplicate_post_link\u2019, 10, 2 ); after the two other filters.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial, we will use a function that will allow you to duplicate a WordPress post without using a plugin. Admins can easily duplicate a draft to save time.<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/how-to-duplicate-articles-without-plugins\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":5087,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[318],"class_list":["post-5085","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-functions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=5085"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5085\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/5087"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=5085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=5085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=5085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}