Pull first_post_likes based on known thread_id

LPH

Well-known member
The thread_id is known and I'd like to pull the first_post_likes based on that thread_id.

This code does the above through an SQL statement. Since I'm trying to learn the "XenForo way" ... the next step is to take this code and change it to using XenForo_Model_Thread and getThreads or should it be getThreadsID?

Here is working code:

PHP:
<?php

/**
* Filename: class-xenword-postratings.php
*
* Description: Shows the post ratings of the first post in a thread on the WordPress post.
*
* @since 2.5.1.01
*        Initial release
*
*/

/**
* This section is for security. Do not modify this part:
* @ignore
*/

if ( ! defined( 'ABSPATH' ) ) exit;

/**
* Pull the likes from the first_post likes in the xf_thread table based on the thread_id
* then return the number of likes to the WordPress $content
*
* @param $content
*
* @return string
* @throws Zend_Exception
*/
function show_postratings( $content ) {

    /** Load the XenWord options set in XenWord settings panel */
    $xenword_options = get_option( 'xenword_options' );

    if ( $xenword_options['use_postratings'] == true ) {

        if ( is_singular() && is_main_query() ) {

            $post_id = '';

            $post = get_post( $post_id, ARRAY_A );
            if ( $post['thread_id'] != '0' ) {

                // Show Post Ratings

                $thread_id = $post['thread_id'];

                $first_post_id_qry = "

                    SELECT `thread_id`, `first_post_likes` FROM `xf_thread`
                    WHERE `thread_id` = $thread_id

                ";

                $xen_post_id = XenForo_Application::get( 'db' )->fetchAll( $first_post_id_qry );

                $xen_first_post_likes= $xen_post_id[0]['first_post_likes'];

                $xenword_postratings = '
                    <span style="font-size: x-small; float: right">Post Ratings:<br />'
                    . $xen_first_post_likes
                    . ' likes</span>
                    ';

                $content .= $xenword_postratings;

            }
        }
    }

    /** return the WordPress post content with the post ratings included */

    return $content;

}

add_filter( 'the_content', 'show_postratings' );

// End class-xenword-postratings.php

The place to focus:

PHP:
                $thread_id = $post['thread_id'];

                $first_post_id_qry = "
                    SELECT `thread_id`, `first_post_likes` FROM `xf_thread`
                    WHERE `thread_id` = $thread_id
                ";

                $xen_post_id = XenForo_Application::get( 'db' )->fetchAll( $first_post_id_qry );

I'm trying to learn this one from scratch rather than copy pasting from previous pulling of thread content.

Somehow I want to grab XenForo_Model_Thread::FETCH_FIRSTPOST, correct?
 
Well, I'm kinda a novice so my advice might be wrong but as far as the query itself goes you should use fetchOne right?

I mean, you could create the model (XenForo_Model::create) and use a function from the model and I guess that'd be the right way to do it, I can't take a look at which function you'd need though, but you could just make your own model function if none exactly match what you're going for.
 
Yes, the model would be created first but I'm trying to figure out whether to use XenForo_Model_Thread::FETCH_FIRSTPOST or if the first_post_likes can be pulled a different way. I'm very weak on XF models and usually copy from previous work. This time I'm trying to reason things out.
 
This should work for you:

PHP:
$threadModel = XenForo_Model::create('XenForo_Model_Thread');
$thread = $threadModel->getThreadById($threadId);
Zend_Debug::dump($thread['first_post_likes']);
 
@Daniel Hood - thank you.

Interesting that PhpStorm doesn't show getThreadById in the dropdown for that model. "Method getThreadById not found in XenForo_Model" -- which is true because it should pull from XenForo_Model_Thread. A setting must not be right :(

Oh - and good one on Zend_Debug since I'm used to var_dump !
 
Interesting that PhpStorm doesn't show getThreadById in the dropdown for that model. "Method getThreadById not found in XenForo_Model" -- which is true because it should pull from XenForo_Model_Thread. A setting must not be right :(
In order for it to do that, you have to do:

PHP:
/* @var $threadModel XenForo_Model_Thread */
            $threadModel = XenForo_Model::create('XenForo_Model_Thread');

phpstorm can't just 'know' which model you're loading like that, you have to give it a hint.
 
@Daniel Hood

OMG. A simple comment allows all the methods to be shown !!

I had no clue ... this is going to change so much of my code. Wow. :cool:

Thank you so much. This simple thing opens a door to a whole new way of coding. (y)
 

Attachments

  • Screen Shot 2015-04-28 at 7.13.24 PM.webp
    Screen Shot 2015-04-28 at 7.13.24 PM.webp
    30.8 KB · Views: 9
  • Screen Shot 2015-04-29 at 4.33.57 AM.webp
    Screen Shot 2015-04-29 at 4.33.57 AM.webp
    86.2 KB · Views: 10
Top Bottom