4

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 [title] => Main ... pipework. [name] => nr-60 [excerpt] => [content] => The ... external. [code] => 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.

3
  • 1
    Do you have enough permissions to edit that post?
    – Max Yudin
    Jan 17, 2018 at 11:08
  • You beauty. Spent so long tweaking with the front end and not visiting admin I had been logged out. Thanks Jan 17, 2018 at 11:10
  • I've created the answer for generations to come.
    – Max Yudin
    Jan 17, 2018 at 11:34

2 Answers 2

4

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.

1
  • 2
    I need post link inside ajax automatization process without any permission, the edit link will be sent via email. There should be an option to get it without checking for permissions?! if the user doesnt have permission, he's not gonna be able to see the link anyway. For my case I had to use admin_url( 'post.php?post='. $id .'&action=edit', 'https' )
    – trainoasis
    Aug 2, 2019 at 10:05
1

This might be one of those times when certain functions work better as part of the loop. Although you're using a loop, you're not actually using the full WP loop functionality.

Try adding 'posts_per_page'=> 1 to your $args to get just the one post, and then replacing $questionpost = $observations->posts[0]; with the missing loop construct, ie: while($observations->have_posts()) : $observations->the_post(); (not forgetting the closing endwhile; of course). This will then allow you to use the normal in-loop functions (eg, get_the_ID(), get_the_title(), etc) and try using get_edit_post_link() without passing the ID to it.

2
  • I moved away from the loop because I only really want a single random post. Anyway. I was being sloooow - I'd spent so long focused on the front end I had been logged out of admin and not noticed. Thanks for the ideas thou. Jan 17, 2018 at 11:17
  • I'm dressing down my old posts and I've found some bad non-siimple-english ones. Can I help you again? There could be a better solution. Let us share it.
    – Max Yudin
    Jun 9, 2019 at 20:20

Your Answer

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

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