XF 2.0 Grab First Post ID for Post\Editor

LPH

Well-known member
The following code works but maybe there is a better way to get the first_post_id for the Post\Editor. Currently, the thread_id is known and the first post is to be edited.

PHP:
/**
 * @param $post
 *
 * @return bool
 */
public function edit( $post ) {

   $message   = get_post_field( 'post_content', $post->ID );
   $content_except = get_post_field( 'post_excerpt', $post->ID );
   $excerpt_length = $this->options['xf_excerpt_length'];

   // Get thread in order to get first post id
   $thread_ID = get_post_meta($post->ID, 'thread_id', true);

   if ( ! empty( $thread_ID ) ) {
      return false;
   }

   $thread = \XF::app()->finder('XF:Thread')->where('thread_id', $thread_ID)->fetchOne();
   $firstPost = \XF::app()->finder('XF:Post')->where('post_id', $thread['first_post_id'])->fetchOne();

   /** @var \XF\Service\Post\Editor $editor */
   $editor = \XF::app()->service('XF:Post\Editor', $firstPost);

   $editor->setMessage( $message );

   $editor->save();
}

Is there a more efficient way to edit the first post outside of XenForo 2?
 
That's pretty much fine, though:
  1. $thread->FirstPost will have what you want.
  2. As of beta 7, you would want to make your edit be noted as automated by calling setIsAutomated on the editor service.
 
Last edited:
Thank you.

$thread->FirstPost will have what you want

If I recall correctly, this failed with an error that the information was not in a post entity format. I’ll try tomorrow.

As of beta 7, you would want to make your edit be noted as automated by calling setIsAutomated on the editor serve

Im not sure what this means and will update to beta 7 to read the code.
 
Top Bottom