Closed JuhG closed 7 years ago
The original idea was to add some extra functionality without hurting anything existing. When I know I need only one post if feels overwhelming to create a collection, but it's the best option right now.
When I needed the last item of a post type I instantly tried to write:
$last_event = rila_post(array(
'post_type' => 'event'
));
Because that's what rila_post does, returns exactly one post.
You're right about the exception. If the returned array is empty, it should instantly return false or null. Otherwise I think it should work like above, right now it doesn't accept an array, so it doesn't conflict with anything, but maybe it's not intuitive enough. I accept that you don't like it.
I like both of your suggestions, rila_posts()
and first()
/ last()
. They are more in line with the framework. Maybe an extra suggestion to that, rila_posts could receive post type as a default argument.
After a couple of commits, you can actually use the shortcuts, so that the following code actually works:
rila_posts()->type( 'event ')->first()
Just FYI, this is something I had forgotten and it's the rila
function. Its documentation is not ready yet, but it's a function that accepts a lot of argument types/"selectors". If it receives no arguments, it returns a new posts collection, so the example above can be changed to the following now:
rila()->type( 'event' )->first()
Hi @JuhG,
I don't completely approve this implementation for a few reasons:
Missing_Object_Exception
. Even though this could be handled externally, many people will forget to do it properly and would get an ugly exception if, let's say, a page gets deleted. It would be cooler if the function can handle the following usage:if( $post = rila_post( $args ) ) { ... }
.There are a few catches though:
rila_posts()
,rila_terms()
and etc. Then you can go ahead withif( $post = rila_posts()->type( 'event' )->at( 0 ) ) { ... }
at()
method currently does not check if an element exists. We need to modify it to returnfalse
ornull
when there is no post.first()
andlast()
methods (along with others typical collection methods). Then we would be able to haveif( $post = rila_posts()->type( 'event' )->first() ) { ... }
.One last concern would be the amount of items (posts in this case), which are being retrieved.
first()
could be implemented in a way that clones the original collection and sets the limit to 1 post, so we don't get an additional DB query just for that post.last()
on the other hand, would also reverse the order.What are your thoughts on this?