Skip to main content
more specific
Source Link

TLDR: I ended up using the the_post hook to add the definitions as the posts are setup. This of course costs another trip to the database for every post but dealing with the duplicate entries proved too difficult.

Extending the WP_Query Class caused more trouble than it was worth. But forFor those interested..., however, here's how I did itextended the WP_Post Class to roll the duplicate posts up into one. I

I suspect this approach is not recommended.

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

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.

I ended up using the the_post hook to add the definitions as the posts are setup. This of course costs another trip to the database for every post but dealing with the duplicate entries proved too difficult.

For those interested, however, here's how I extended the WP_Post Class to roll the duplicate posts up into one.

I suspect this approach is not recommended.

Post Undeleted by guardiancrescent
I found a simple solution
Source Link

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.

I came up with this. I just wonder if I'm approaching this the right way.

I came up with this. I just wonder if I'm approaching this the right way.

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.

Post Deleted by guardiancrescent
Source Link

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();
}

I came up with this. I just wonder if I'm approaching this the right way.