Fetch post ids in a single thread without fetching first post

LPH

Well-known member
The XenForo_Model_Post has several different means to grab posts based on $threadId.
  • getPostsInThread
  • getPostsInThreadSimple
  • getPostIdsInThread
However, all of the methods return the first post. Instead, an array of all the posts in a reply without the first post is needed.

This is the code developed up to the point of realizing the first post is included.

PHP:
            // Get WordPress post_id for Post
            $wp_post = get_post( $post_id );

            // Get XenForo thread_id based on WP post_id
            $threadId = get_post_meta( $wp_post->ID, 'thread_id', true );

            // Get all posts based on thread_id

            /** @var  $postdModel XenForo_Model_Thread */
            $postModel = XenForo_Model::create( 'XenForo_Model_Post' )->getModelFromCache( 'XenForo_Model_Post' );

            /** @var $postModel XenForo_Model_Post $posts */
            $postIds = $postModel->getPostIdsInThread($threadId, $ordered = true);

            foreach ( $postIds as $postId ) {
               // Returns ALL post ids
                echo '<pre>' . var_export( $postId ) . '<br /></pre>';
            }

The question really is ... can I simply use the $key === 0 ?

PHP:
            foreach ( $postIds as $key => $postId ) {
                if ( $key === 0 ) continue;
                echo '<pre>' . var_export( $postId ) . '<br /></pre>';
            }

This appears to work but will there be any problems with it in the future?

Does anyone have any suggestions?
 
Last edited:
So, you are attempting to remove the first post ID in the returned array?

Your method will work, as the array is not keyed so the first index will not be anything but 0. However, if you wanted to ensure it (incase you did ever receive a keyed array -- you won't), you can use array_shift to remove the first element completely:

PHP:
           // Get WordPress post_id for Post
 $wp_post = get_post( $post_id );

 // Get XenForo thread_id based on WP post_id
 $threadId = get_post_meta( $wp_post->ID, 'thread_id', true );

 // Get all posts based on thread_id

 /** @var $postdModel XenForo_Model_Thread */
 $postModel = XenForo_Model::create( 'XenForo_Model_Post' )->getModelFromCache( 'XenForo_Model_Post' );

 /** @var $postModel XenForo_Model_Post $posts */
 $postIds = $postModel->getPostIdsInThread($threadId, $ordered = true);
array_shift($postIds);

 foreach ( $postIds as $postId ) {
 // Returns ALL post ids bar the first one in the array
 echo '<pre>' . var_export( $postId ) . '<br /></pre>';
 }
 
Back
Top Bottom