Support » Developing with WordPress » Get edit attachment link

  • Resolved El Asador Aleman


    Hi there!

    I am trying to solve a problem that seems trivial, but I am spinning around quite some time now, so I ask for help.

    $prev_post = get_previous_post();
    $prev_post_ID = $prev_post->ID;
    get_edit_post_link( $prev_post_ID )

    With this tree lines of codes I retrieve the link to the next post in the admin.

    For some unknown reason get_previous_post() ignores attachment. In the ductus of WP atachment are also posts, but it seems that in this particular case they got filtered out.

    So the one millio dollar question is, how do Iretrieve the same information for attachments?

    I would be very thankful for some support here.

    Best regards

    El asador aleman

Viewing 4 replies - 1 through 4 (of 4 total)
  • Greetings. Sorry, I’m not quite sure what you mean by attachment? What kind of attachments are you missing?

    Hello Stan! By attachment I mean the posts, with post_type “attachment”, which are handled in the media library. I am doing a little meta box to offer the functionality to navigate the posts without leaving the edit mask of the posts.

    Works fine for the post _types “posts” and “pages” with the coding above but not for the post with post_type “attachment”. So I wanted to know under which circumstances get_edit_post_link() works as well for attachments or if there is another function() in WP that do the same job for attachments and I am not able to find it.

    Hope I made it clear this time.

    Thanks for your Reply.

    Best regards

    Moderator bcworkz


    get_previous_post() in theory should work. It queries for a post of the same type as the current with the next earliest date. So if the current post is an attachment, it queries for the next earliest attachment post. But it fails to return anything!

    The query it runs for attachments looks something like this:
    SELECT p.ID FROM wp_posts AS p WHERE p.post_date < '2017-09-01 12:00:00' AND p.post_type = 'attachment' AND ( p.post_status = 'publish' OR p.post_status = 'private' ) ORDER BY p.post_date DESC LIMIT 1
    It then gets and returns the post object from the returned ID.

    Looks good, right? It’s not. Attachments never have “publish” status, it’s always “inherit”! You need to filter “get_previous_post_where” and when the passed post object’s type is “attachment”, replace ‘publish’ in the passed WHERE clause with ‘inherit’. Only then will get_previous_post() work with attachments. Do the same for next posts.

    This appears to be a bug in WP, but only if get_previous_post() is intended to work with attachments. I’m not so sure that it is. In any case, a workaround like I suggest is easily implemented.

    Thanks bcworkz for your answer which gave me a hint to solve the problem.

    It’s actually not a bug but a “clumsy” designed function. The function behind all is get_adjacent_post().

    get_previous_post() and get_next_post() call this function in order to get the results. And this function builds up a “WHERE” condition in the SQL statement that keeps with this condition “p.post_status = ‘publish'” pretty much everything outside which is not page or post.

    $where .= ” AND ( p.post_status = ‘publish'”;
    foreach ( (array) $private_states as $state ) {
    if ( current_user_can( $read_private_cap ) ) {
    $where .= $wpdb->prepare( ” OR p.post_status = %s”, $state );
    } else {
    $where .= $wpdb->prepare( ” OR (p.post_author = %d AND p.post_status = %s)”, $user_id, $state );
    $where .= ” )”;
    } else {
    $where .= ” AND p.post_status = ‘publish'”;

    If you re going to change that to condition to “p.post_status != ‘auto-draft'” it shows you nearly everything. Even custom post types. Obviously you have to rename the function to avoid a redeclaration.

    Of course I don’t have the full picture of the code base but probably it would make sense to make an enhancement request to the team.

    Best regards and thanks again for the lead.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get edit attachment link’ is closed to new replies.