Skip to main content
2 of 3
I found a simple solution

TLDR: I ended up using the the_post hook to add the definitions as the posts are setup.

function add_definitions( $post ) 
{
    if ( 'word' != $post->post_type ) ) return;

    global $wpdb;

    $query  = "SELECT * FROM {$wpdb->prefix}definitions ";
    $query .= "WHERE word_id = " . $post->ID;

    $post->ideas = $wpdb->get_results( $query, ARRAY_A );
}
add_action( 'the_post', 'add_definitions' );

Extending the WP_Query Class caused more trouble than it was worth. But for those interested... here's how I did it. I suspect this approach is not recommended.

class Words_Query extends WP_Query
{
    function __construct( $args = array() )
    {
        $args = array_merge( $args, array(
            'post_type' => 'word'
        ) );

        parent::__construct( $args );

        $this->posts = parent::get_posts();

        if ( count( $this->posts ) > 0 )
        {
            $prev_word = null;
            foreach ( $this->posts as $key => &$post ) 
            {
                if ( !property_exists( $post, 'definitions' ) ) $post->{'definitions'} = array();

                $definition = array(
                    'definition_id' => $post->definition_id,
                    'definition' => $post->definition,
                    'note' => $post->note,
                );

                if ( is_object( $prev_word ) )
                {
                    if ( $prev_word->ID == $post->ID )
                    {
                        // update the previously inserted post
                        $prev_word->definitions[] = $definition;

                        // remove the current post
                        unset( $this->posts[$key] );

                        continue;
                    }
                }

                $post->definitions[] = $definition;
                $prev_word = $post;
            }
        }

        $this->posts = array_values( $this->posts );
    }

    var $posts = array();
}