So inside my plugin I have the following code. It gets a question from a custom_post. I’m processing it here so that further updates can be done by AJAX/JSON and the page only has to be configured for one type of data source.

$observations = new WP_Query($args);  
if ( $observations-> have_posts() ) :
    $questionpost = $observations->posts[0];
    $question = array (
        'id' => $questionpost->ID,
        'title' => $questionpost->post_title,
        'name' =>  $questionpost->post_name,
        'excerpt' => $questionpost->post_excerpt,
        'content' => $questionpost->post_content,
        'code' => get_post_meta( $questionpost->ID, 'code', true ),
        'edit_link' => get_edit_post_link($questionpost->ID),
    );
    if ( has_post_thumbnail($questionpost->ID) ) {
        $question['thumbnail'] = get_the_post_thumbnail( $questionpost->ID, 'full', array('class' => 'card-img-top')); 
    } else {
        $question['thumbnail'] = get_template_directory_uri()."/img/no-image.png";
    }
    print_r($question);
}

It all works fine except for the get_edit_post_link – here is print_r dump:

Array ( [id] => 208 get_edit_post_link() not working as expected when passed id in plugin => Main ... pipework. [name] => nr-60 [excerpt] => [content] => The ... external.  => NR [edit_link] => [thumbnail] => http://.../img/no-image.png ) 

https://codex.wordpress.org/Function_Reference/edit_post_link suggests we can pass an ID so I don't see why this is blank.

2 Answers
2

According to the get_edit_post_link() function source this can happen in following conditions:

  • there is no such post;
  • there is no such post type;
  • you don't have enough permissions to edit the post;
  • _edit_link was changed during post type registration.

The first two are not the case since the ID is available. The fourth is a bad practice: not for general use — core developers recommend you don't use this when registering your own post type.

In this case, the user doesn't have enough permissions. According to the OP's comment under the question, he had been logged out, which is the same sort of thing.

Leave a Reply

Your email address will not be published. Required fields are marked *