0

I have some custom post types. Let's call them apple, banana and pear.

On the frontend I want to add a link for the authorized users to edit the custom post. That is not a problem - that works with edit_post_link.

For deletetion I could create post.php?post=[post_ID]&action=trash, but it seems that a nonce is needed. Which is good. I don't want the user to accidentally delete the post. When I use a nonce, I think the post gets deleted - straight forward.

That's not what I want, instead I want another link to be added on the frontend of the page which leads to the edit view in the dashboard - let's label it "manage post". Using edit.php?post_type=[apple|banana|pear] is quite nice, but I want to generate a link on the frontend to an edit view where only the specific post is listed, i.e. the view is limited to a single ID.

What is the correct syntax to filter the edit view? edit.php?post_type=apple&post=1337 or &post_id=1337 similar did not work. How can this be accomplished, if it all?

Edit: I added a picture to clarify what I mean. I want to reduce the view to show only a specific post, e.g. here the first one with ID=1234.

Edit View

7
  • 1
    Are you trying to implement this filter because the delete link doesn't work? Just note that you can use get_delete_post_link( $id ) which will return URL (with nonce) for trashing the post (otherwise get_delete_post_link( $id, '', true ) will immediately delete it) Oct 26 '16 at 10:50
  • @TheDeadMedic: thank you for your comment. That is a good note. It's not the only reason, as I also want to display the post's context, e.g. connected posts, and other columns in that view (even if that's not applicable for the screenshot). So I don't want to focus on the "deletion" use case only, but thanks for the hint! Oct 26 '16 at 10:54
  • @TheDeadMedic: BTW, do you know why there is a edit_post_link and no delete_post_link or trash_post_link as well. I mean - where you can simply add a label, text before and after and don't have to care about capabilities on your own? Oct 26 '16 at 10:57
  • 1
    I see what you mean - tbh it's not that hard to just $url = get_delete_post_link( $id ); if ( $url ) printf( '<a href="%s">Delete</a>', $url ); Oct 26 '16 at 11:08
  • 1
    Note that get_delete_post_link still does the capability checking and will return null if the current user can't delete it Oct 26 '16 at 11:09
0

You can implement the filter yourself:

add_filter( 'request', function ( $vars ) {
    if ( is_admin() && $id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT ) )
        $vars['post_in'] = [ $id ];

    return $vars;
}); 

Now if you pass a post param in the URL with the ID it will only show that post.

1
  • Thank you for your help. I have appended your code to my plugin code. The plugin is activated, though when opening edit.php?post_type=apple&post=1234 or edit.php?post=1234 the result is not filtered properly. Can you give me a hint how to debug? Oct 26 '16 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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