Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Cannot use Timber\Post::factory when extending Post class as the method is private #2873

Closed
montagmorgens opened this issue Jan 3, 2024 · 4 comments · Fixed by #2923
Labels
2.0 has-patch help wanted Do you know computer? Then lend a hand and have your code live in infamy!

Comments

@montagmorgens
Copy link

Expected Behavior

When extending a Timber\Post, I should be able to access the factory() method.

I'm trying to override the children() method which needs to have access to the factory.

Actual behavior

Timber\Post::factory() is private and cannot be accessed by inhertitance.

Steps to reproduce behavior

  1. Create a class that extends Timber\Post
  2. Try to access the factory method
  3. It returns false instead of a PostFactory object.

Notes

No response

What version of Timber are you using?

Timber 2.0

What version of WordPress are you using?

WordPress 6.4.2

What version of PHP are you using?

PHP 8.1

How did you install Timber?

Installed or updated Timber through Composer

@Levdbas
Copy link
Member

Levdbas commented Jan 4, 2024

Related: #2813.

@nlemoine , could you chime in if @montagmorgens has a valid point or is there another recommended way to handle children overwrites.

@Levdbas Levdbas added the 2.0 label Jan 4, 2024
@nlemoine
Copy link
Member

nlemoine commented Jan 4, 2024

@montagmorgens Can you provide more details on your use case? The factory is just meant to make post queries, I'd like to have more context before considering making factory protected.

@montagmorgens
Copy link
Author

@nlemoine Our use case is to include children posts with post_status private. Since there's no filter hook which would allow us to change the query in the children() method, we tried to override the method.

@gchtr
Copy link
Member

gchtr commented Jan 25, 2024

@montagmorgens Instead of using $this->factory(), you could probably use Timber::get_posts() in your custom children() method as a quick fix.

Before

return $this->factory()->from(\get_children($query));

After

return Timber::get_posts(\get_children($query));

We could probably provide a better solution to update the query by using a filter or adding an optional parameter with query parameters that overwrite the default ones. A pull request would be welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.0 has-patch help wanted Do you know computer? Then lend a hand and have your code live in infamy!
Projects
None yet
4 participants