4

I'm using this code to have a duplicate post function in WordPress Admin. However, when I add the filter for a custom post type, like this:

add_filter( 'directory_row_actions', 'rd_duplicate_post_link', 10, 2 );

(The Custom Post Type has a registered name of directory) - it doesn't add it to the action row underneath the entry title. When I do it for the posts or pages, like this:

add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );

it works perfectly. I've been reading that post_row_actions has been deprecated but I can't find anywhere that says it's replacement. Does anyone know how to get this working for my Custom Post Type?

5
  • 1
    Please add all relevant code to your question. Sep 11, 2014 at 12:53
  • it's on the link I posted on the first line (rudrastyh.com/wordpress/duplicate-post.html), but it's quite long so I thought it best to leave it there. I can add it in if you want though?
    – Tom Oakley
    Sep 11, 2014 at 13:06
  • You might want to strip it down to the necessary parts, but generally outgoing link sources can vanish and than there is no information available and the question in the future more or less useless. Sep 11, 2014 at 13:12
  • ok I'll do that, your explanation makes sense, thanks @ialocin :)
    – Tom Oakley
    Sep 11, 2014 at 13:33
  • Looking at the source for "wp-admin/includes/class-wp-posts-list-table.php", it calls page_row_actions for hierarchical post types, and post_row_actions for all others (there's no post_type-specific filter), so one of them should work for you. (You'll have to check the passed in $post->post_type for varying behaviour.) Nothing in the code anyway about deprecation.
    – bonger
    Sep 11, 2014 at 17:35

2 Answers 2

13

As @bonger commented, there is no custom post type filter despite what you've read.

To use this filter for a specific post type, the best way is to use the post_row_actions filter and then test against the passed in $post->post_type.

I've used the code below to add links to the actions row for a specific post type (in this case, myposttype).

This will need to be edited to work for your own post type, and obviously for the new link to actually do anything more code is needed, but this is the idea:

function my_duplicate_post_link($actions, $post)
{
    if ($post->post_type=='myposttype')
    {
        $actions['duplicate'] = '<a href="#" title="" rel="permalink">Duplicate</a>';
    }
    return $actions;
}
add_filter('post_row_actions', 'my_duplicate_post_link', 10, 2);
1
  • Also, your Custom Post Type must be non-hierarchical. 'hierarchical' => false when declaring your CPT withregister_post_type('MyCustomPostType'...) Check WP Code Reference
    – Braza
    Jul 29, 2020 at 13:39
-1
//Post, page or all register custom post types

function im_duplicate_post_link($actions, $post)
{
    // get all post type which is register
    $posttypes = get_post_types(array('public' => true), 'names', 'and');
    foreach ($posttypes as $post_type)
    {
        $posttype[] = $post_type;
    }
    //check current post type is in "$posttype"  array, if it's in array then display duplicate link.
    if (in_array($post->post_type, $posttype))
    {
        $actions['duplicate'] = '<a href="admin.php?action=im_duplicate_post_as_draft&amp;post=' . $post->ID . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
    }
    return $actions;
}
// this hook for post and custom post type
add_filter('post_row_actions', 'im_duplicate_post_link', 10, 2);
// this hook for page
add_filter('page_row_actions', 'im_duplicate_post_link', 10, 2);
2
  • 2
    Please, explain how your code works and why it answers the question. Only code is not a good answer.
    – cybmeta
    Jan 2, 2015 at 11:02
  • In above code used for duplicate post,page or any custom post type. Jan 2, 2015 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.