Skip to content

2.x Add Timber::get_post_by()#2169

Merged
gchtr merged 27 commits into2.xfrom
2.x-get-post-by
Jun 22, 2020
Merged

2.x Add Timber::get_post_by()#2169
gchtr merged 27 commits into2.xfrom
2.x-get-post-by

Conversation

@gchtr
Copy link
Copy Markdown
Member

@gchtr gchtr commented Jan 12, 2020

Ticket: #2152

Issue

See #2152.

Solution

Add Timber::get_post_by_slug() and Timber::get_post_by_title().
Add Timber::get_post_by().

I chose post_exists() when searching for any post type and get_page_by_title() when searching for specific post types as the functions to retrieve a post by post title, because they seem to be the most direct and performant ones.

Impact

Closes a gap in what Timber::get_post() can’t (and shouldn’t be able) to do in Timber 2.0.

Usage Changes

Instead of Timber::get_post() with a post title or post slug, developers should use one of the two new functions.

Considerations

The two functions always run database queries to get post IDs, which got me thinking about performance. When we use ::build() to create an object, could we eventually use wp_cache_get() and wp_cache_add() in the future to create an internal object cache for Timber objects?

Testing

Yes.

@jarednova
Copy link
Copy Markdown
Member

jarednova commented Jan 12, 2020

@gchtr I know you're going to scream, but having just spent time with #2160 and #2167, I'm thinking of Timber::get_user_by — does it make sense for us to follow the same pattern with:

  • Timber::get_post_by('slug', 'my-cool-slug');
  • Timber::get_post_by('title', "The Old Man and the Sea");

... ? I realize that's piggy-backing on the corresponding WordPress function that isn't matched by Posts. Had you given that any thought?

@gchtr gchtr self-assigned this Jan 12, 2020
@gchtr
Copy link
Copy Markdown
Member Author

gchtr commented Jan 12, 2020

I know you're going to scream

@jarednova Naaah, that’s fine 😄.

To be honest, I didn’t think of this at all yet. But I like it! Let’s get @acobster’s opinion, before I’m going to change this.

@acobster
Copy link
Copy Markdown
Collaborator

@jarednova @gchtr I like it! It's more consistent and keeps the surface area smaller.

Copy link
Copy Markdown
Collaborator

@acobster acobster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main thing left to do is to convert consolidate both methods to ::get_post_by(). Also just need to make sure we take the new/correct return value into account.

@gchtr gchtr changed the title 2.x Add Timber::get_post_by_slug() and Timber::get_post_by_title() 2.x Add Timber::get_post_by() Jan 18, 2020
Copy link
Copy Markdown
Member

@jarednova jarednova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gchtr looking good! There's a discrepancy between the test results and what I think we're expecting. Right now the behavior with multiple results is ...

  • get_post_by('slug' => returns the newest post based on WP's default sort behavior
  • get_post_by('title' => returns the oldest post based on the behavior in get_post_by_title

I think these should be consistent in their behavior (lowest ID? oldest? newest?). Standard use case I imagine to be that a dev is trying to get something like privacy-policy. You could imagine another author (for some stupid reason) using this as a slug or title for a future post which would then break the linkage. Therefore, I'd think it makes sense to always return the oldest match.

Happy to take a whack at this, but wanted to run that by you first

@gchtr
Copy link
Copy Markdown
Member Author

gchtr commented Feb 8, 2020

@jarednova Yeah, I remember reading that in the function description, but then the tests ran successfully, so I thought it’s fine like it is. Apparently it isn’t and I must have missed something. Thank you for catching this! I’d be happy if you could take over to solve this 👍.

@gchtr gchtr assigned jarednova and unassigned gchtr Feb 8, 2020
@jarednova
Copy link
Copy Markdown
Member

np! will take care of this in the next couple days

jarednova
jarednova previously approved these changes Feb 10, 2020
@jarednova
Copy link
Copy Markdown
Member

@gchtr this is now ready for your final review!

@jarednova jarednova assigned gchtr and unassigned jarednova Feb 15, 2020
When we check for the date, the ID of the post we expect to be returned needs be higher than the ones that should be ignored in order for the test to run correctly
This makes it possible to order by anything we want
@gchtr
Copy link
Copy Markdown
Member Author

gchtr commented Feb 16, 2020

Thanks @jarednova for working on this! Your changes actually pointed me in the right direction and made me rethink how we should handle cases when multiple posts are found. I created another pull request in #2195 that we need to discuss first.

gchtr added 2 commits April 26, 2020 14:03
# Conflicts:
#	tests/test-timber.php
# Conflicts:
#	docs/v2/upgrade-guides/2.0.md
@codecov-commenter

This comment was marked as outdated.

@jarednova
Copy link
Copy Markdown
Member

@gchtr just wanted to make sure this didn't get lost in the cracks. With #2195 merged, is this also ready?

@gchtr
Copy link
Copy Markdown
Member Author

gchtr commented Jun 17, 2020

@gchtr just wanted to make sure this didn't get lost in the cracks. With #2195 merged, is this also ready?

@jarednova Yes, this one’s ready!

@gchtr gchtr requested a review from jarednova June 18, 2020 14:53
@jarednova
Copy link
Copy Markdown
Member

We're good here! The test failures are related to static analysis and a failure in an unrelated issue in the 2.x branch

@gchtr gchtr merged commit 7fdd887 into 2.x Jun 22, 2020
@gchtr gchtr deleted the 2.x-get-post-by branch June 22, 2020 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants