• Resolved Cezar Ayran

    (@ayrancd)


    Hi,

    Is there a way to call the duplicate action of this plugin using PHP? Like, passing the post ID and getting the new post draft ID? Tks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Cezar Ayran

    (@ayrancd)

    I’m trying to do something like:

    $rr = do_action("admin_action_duplicate_post_new_draft", 27572);

    But I have to pass the variable post with the value 27572 using GET or POST and the variable action with value duplicate_post_new_draft.

    Is there a way to do that?

    Thread Starter Cezar Ayran

    (@ayrancd)

    Just found out they have a function for that:

    $new_post_id = duplicate_post_create_duplicate(27572, "draft");

    But I am getting this error:

    Fatal error: Uncaught Error: Call to undefined function duplicate_post_create_duplicate()

    Plugin Support Michael Tiña

    (@mikes41720)

    Hi @ayrancd

    Please do check out our documentation for the Yoast Duplicate Post plugin in order to locate the specific filter or action you need for your use-case:

    Thread Starter Cezar Ayran

    (@ayrancd)

    @mikes41720 I saw that article but I couldn’t make the action dp_duplicate_post work…

    Do I need the do_action as well? How do I get the new post ID? Can you pls post a snippet code?

    Did you see the php error when I call that function also found in one of your articles??

    Thread Starter Cezar Ayran

    (@ayrancd)

    Just found a solution without using a plugin:

    function duplicatePost($post_id, $title, $block, $execution){
    	$post = get_post($post_id);
    	$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'    => 'draft',
    	      'post_title'     => $title,
    	      'post_type'      => $post->post_type,
    	      'to_ping'        => $post->to_ping,
    	      'menu_order'     => $post->menu_order
        	);
        	$new_post_id = wp_insert_post($args);
    
        	global $wpdb;
        	$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM ydo_postmeta WHERE post_id = $post_id");
     	if (count($post_meta_infos)!=0){
     		$sql_query = "INSERT INTO ydo_postmeta (post_id, meta_key, meta_value) ";
     		      foreach ($post_meta_infos as $meta_info) {
    	        		$meta_key = $meta_info->meta_key;
    	        		if( $meta_key == '_wp_old_slug' ) continue;
    	        		$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);
     	}
        	return $new_post_id;
    }
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Calling plugin action using PHP’ is closed to new replies.