Skip to content

2.x Update function signature for Term::posts()#2258

Merged
acobster merged 13 commits into2.x-posts-apifrom
2.x-term-posts
Oct 7, 2020
Merged

2.x Update function signature for Term::posts()#2258
acobster merged 13 commits into2.x-posts-apifrom
2.x-term-posts

Conversation

@gchtr
Copy link
Copy Markdown
Member

@gchtr gchtr commented Jun 6, 2020

Tickets:

Issue

I almost forgot again about what we discussed in the linked Tickets above.

Solution

This pull request updates the function signature for Term::posts() to use the same pattern that we use for a lot of other functions.

// Old
function posts( $numberposts_or_args = 10, $post_type_or_class = 'any', $post_class = '' );

// New
function posts( $query_args = [] );

Impact

There’s no backwards compatibility, but I added checks when the wrong parameters are passed.

Because I changed the Term::posts() function, I had to update the Term::get_post() function as well. This will trigger a doing-it-wrong notice now. Because we can no longer support the former arguments, this is not a formal deprecation anymore.

Usage Changes

Old usage

In PHP:

$genre->posts( -1, 'book' );

And in Twig:

{% for book in genre.posts(-1, 'book) %}

New usage

In PHP:

$genre->posts( [
    'post_type'     => 'book',
    'post_per_page' => -1,
    'orderby'       => 'menu_order'
] );

And in Twig:

{% for book in genre.posts({
    post_type: 'book',
    posts_per_page: -1,
    orderby: 'menu_order'
}) %}

Considerations

None.

Testing

There are tests that are still failing because of the class parameter that is not supported anymore. We should probably wait until the post factory is finished and update the tests then.

@gchtr gchtr added the 2.0 label Jun 6, 2020
@gchtr gchtr requested review from acobster and jarednova June 6, 2020 15:51
@gchtr gchtr requested a review from pascalknecht as a code owner June 6, 2020 15:51
@jarednova
Copy link
Copy Markdown
Member

The code looks good. As for the three failing tests, here they are for easy reference:

1) TestTimberTerm::testGetPostsNew
Failed asserting that Timber\Post Object (...) is an instance of class "TimberPostSubclass".

/srv/www/timber/tests/test-timber-term.php:222

2) TestTimberTerm::testPostsWithCustomPostTypeAndCustomClassMap
Failed asserting that Timber\Post Object (...) is an instance of class "TimberPostSubclass".

/srv/www/timber/tests/test-timber-term.php:279

3) TestTimberTerm::testPostsWithCustomPostTypePageAndCustomClass
Failed asserting that Timber\Post Object (...) is an instance of class "page".

/srv/www/timber/tests/test-timber-term.php:309

... looks pretty straightforward to solve in conjunction with Factories

@gchtr
Copy link
Copy Markdown
Member Author

gchtr commented Jun 10, 2020

... looks pretty straightforward to solve in conjunction with Factories

Yes, but first the Post Factories need to be used in Timber::get_posts(). #2145 should be merged first.

@gchtr gchtr added the wip Work in Progress label Aug 14, 2020
@acobster acobster force-pushed the 2.x-factories branch 3 times, most recently from 23c3421 to b0d086a Compare August 17, 2020 22:13
@acobster
Copy link
Copy Markdown
Collaborator

@jarednova this is looking good to me, but I'm wondering if you think adding an $options arg for forwarding to Timber::get_posts() would make sense here?

@jarednova jarednova removed the blocked label Sep 30, 2020
@jarednova
Copy link
Copy Markdown
Member

@acobster agreed, while things are fresh and open let's add that in.

@gchtr
Copy link
Copy Markdown
Member Author

gchtr commented Oct 5, 2020

@jarednova this is looking good to me, but I'm wondering if you think adding an $options arg for forwarding to Timber::get_posts() would make sense here?

Cool, I added the parameter in 263bed9.

@gchtr gchtr mentioned this pull request Oct 5, 2020
15 tasks
@acobster acobster changed the base branch from 2.x-factories to 2.x-posts-api October 7, 2020 17:39
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.

We've discussed these changes over on #2316. Merging.

@acobster acobster merged commit 941e535 into 2.x-posts-api Oct 7, 2020
@acobster acobster deleted the 2.x-term-posts branch October 7, 2020 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.0 wip Work in Progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants