1

I'd like to create a subclass of WP_Post and add some "model functionality" to it. How can i force WP to create objects of that child class instead of WP_Post itself, when i query for my custom post type?

Example:

Let's assume i have two custom post types: Book and Review. Each Book can have many Reviews. On my Book, I want a method to sum up all its reviews. I'd define the following class:

class Book extends WP_Post
{
    public function reviewsSummary()
    {
        // Retrieve all reviews for $this book
        // Sum up their ratings
        // Return that sum
    }
}

Is there a way, for example when calling register_post_type(), to force WordPress into casting all posts of type "book" into my Book class instead of WP_Post?

Could look something like this:

register_post_type('book', [
    …,
    'class' => Acme\Models\Book::class
]);
8
  • You can override methods and properties of parent class that exist already looking here: core.trac.wordpress.org/browser/tags/4.5.3/src/wp-includes/… Nov 16, 2016 at 10:57
  • I'm not asking how to override a method, but how to make WP turn my posts into objects of a different class than WP_Post
    – jsphpl
    Nov 16, 2016 at 11:08
  • ah, ok, so instead of creating post object using WP_Post class, it would use Book class? For register_post_type, there is no option of overriding WP_Post class in that manner. Even in get_post() function, there is no option to override WP_Post. You could however instead of using get_post(), create function get_book(), and then instead of using WP_Post class, use Book class. I am aware that you want to automatize it that way, however, some things like overriding menu walker class, or widget class are not easy applicable for posts. Nov 16, 2016 at 11:51
  • 1
    register_post_type is used to create or modify the custom post type. Try with walker class. Nov 16, 2016 at 12:03
  • 1
    Oh, and I found now similar case :) resoundingechoes.net/development/extending-the-wp-post-class Nov 16, 2016 at 15:29

1 Answer 1

1

I am not sure, but maybe this is a way for that what you want:

Look here at line 3617 -> query.php

// Convert to WP_Post objects
if ( $this->posts )
    $this->posts = array_map( 'get_post', $this->posts );

if ( ! $q['suppress_filters'] ) {
    /**
    * Filter the raw post results array, prior to status checks.
    *
    * @since 2.3.0
    *
    * @param array    $posts The post results array.
    * @param WP_Query &$this The WP_Query instance (passed by reference).
    */
    $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) );
}

Maybe you can use the filter hook apply_filters_ref_array and run your own function if the post type is a book.

// in your filter function
if ( book ) {
   return array_map( 'get_book', $posts );
}
return $posts;
2
  • Thanks, i'll try that one!
    – jsphpl
    Nov 16, 2016 at 12:27
  • Okay so the array passed to that filter contains WP_Post objects already. But still, it is a place where i can hook in and replace them.
    – jsphpl
    Nov 16, 2016 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.