Posts::get() by postinfo

1 view
Skip to first unread message

Michael C. Harris

unread,
Dec 1, 2009, 11:10:22 PM12/1/09
to habari-dev
I have a content type representing a magazine issue, that are returned
through a custom rewrite rule. It saves the year of publication in the
postinfo table. When displaying the posts, I want to display them in
year order and grouped by years. Is this possible using Posts::get() ?
If not, does anyone have any recommendations on how to do it ?

--
Michael C. Harris, School of CS&IT, RMIT University
http://twofishcreative.com/michael/blog
IRC: michaeltwofish #habari

luke

unread,
Dec 2, 2009, 12:01:14 AM12/2/09
to habari-dev
Mr Harris,

For posterity:

so. There isn't a way that i know of to do it via Posts::get directly
per say, but i wrote a class that orders posts according to some index
set that you provide. so, in my case,i wanted to sort by views, but
views were in an external table so i got the set of post ids in order,
then match them up to posts

Check it out, note that it is _very_ dev, as in, half the code is
probably junk. http://pastoid.com/byd
And to use, you do something like: http://pastoid.com/bye

Originally, the idea was to use natural sort, but something went wrong
with that. I think it had something to do with count limiting or
wanting to restrict to a set of id's as well as sort by them. Anyway.
at some point i'll go back to it.

the nice thing is that, you can supply a list of id's and it will sort
with those first, but you can further refine using standard Posts::Get
syntax and acls are respected

For more context: http://drunkenmonkey.org/irc/habari/2009-12-02#T04-58-09

Cheers!

Michael C. Harris

unread,
Dec 2, 2009, 12:10:08 AM12/2/09
to habar...@googlegroups.com
Thanks Luke. Given that we're both doing something similar that's not
supported by the API, perhaps we should add support at some point.

2009/12/2 luke <lu...@squareweave.com.au>:
> --
> To post to this group, send email to habar...@googlegroups.com
> To unsubscribe from this group, send email to habari-dev-...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/habari-dev

Michael C. Harris

unread,
Dec 2, 2009, 1:58:26 AM12/2/09
to habar...@googlegroups.com
Actually, that screws up pagination, as it only sorts after getting posts.

At the moment I'm calling directly to the db, doing a join on posts
and postinfo and sorting on the year. I still have to deal with
pagination myself, which is less than optimal.

2009/12/2 Michael C. Harris <michael...@gmail.com>:

Michael C. Harris

unread,
Dec 2, 2009, 6:31:20 PM12/2/09
to habari-dev
2009/12/2 Michael C. Harris <michael...@gmail.com>:
> I have a content type representing a magazine issue, that are returned
> through a custom rewrite rule. It saves the year of publication in the
> postinfo table. When displaying the posts, I want to display them in
> year order and grouped by years. Is this possible using Posts::get() ?
> If not, does anyone have any recommendations on how to do it ?

This sorts by postinfo field and handles pagination. Refactor my code :)

public function filter_theme_act_display_imprints( $handled, $theme )
{
$paramarray['fallback']= array(
'{$type}.multiple',
'multiple',
);

// Makes sure the imprint page displays only imprint
$paramarray['user_filters']= array(
'content_type' => Post::type('imprint'),
);

$theme->imprint_page = Post::get( array( 'slug' => 'imprint',
'content_type' => Post::type( 'page' ) ) );

$page = $theme->matched_rule->named_arg_values['page'];

$limit = 5;
$offset = 0;

// Calculate the OFFSET based on the page number
if ( isset( $page ) && is_numeric( $page ) ) {
$offset = ( intval( $page ) - 1 ) * intval( $limit );
}

$params = array(Post::type('imprint'));
$query = <<<SQL
SELECT *
FROM habari__posts as posts
LEFT JOIN habari__postinfo as postinfo
ON posts.id = postinfo.post_id
WHERE posts.content_type = ?
AND postinfo.name = 'year'
ORDER BY postinfo.value DESC
LIMIT $limit
OFFSET $offset
SQL;
$results = DB::get_results( $query, $params, 'Post' );

$theme->imprint_posts = $results;

$theme->act_display( $paramarray );
return TRUE;

drzax

unread,
Dec 3, 2009, 6:11:19 AM12/3/09
to habari-dev
I don't have an immediate use for this, but it's the kind of thing I
know I'll be looking for one day. Accordingly, I think it needs a
better home than this, practically unsearchable (WTF Google??) email
archive. Care to add it to the wiki somewhere appropriate? I'd do it,
but I can't think of the somewhere appropriate right at the moment.
Exercise mashes my brain.

S.

On Dec 3, 9:31 am, "Michael C. Harris" <michael.twof...@gmail.com>
wrote:
> 2009/12/2 Michael C. Harris <michael.twof...@gmail.com>:

Michael C. Harris

unread,
Dec 3, 2009, 2:53:36 PM12/3/09
to habar...@googlegroups.com
2009/12/3 drzax <elver...@gmail.com>:
> I don't have an immediate use for this, but it's the kind of thing I
> know I'll be looking for one day. Accordingly, I think it needs a
> better home than this, practically unsearchable (WTF Google??) email
> archive. Care to add it to the wiki somewhere appropriate? I'd do it,
> but I can't think of the somewhere appropriate right at the moment.
> Exercise mashes my brain.

Will do. I was thinking there needs to be a Dev:Retrieving_Posts page
that mostly focusses on how to use Posts::get().


--

Michael C. Harris

unread,
Dec 4, 2009, 12:21:13 AM12/4/09
to habari-dev
2009/12/3 Michael C. Harris <michael...@gmail.com>:
> 2009/12/2 Michael C. Harris <michael...@gmail.com>:
>> I have a content type representing a magazine issue, that are returned
>> through a custom rewrite rule. It saves the year of publication in the
>> postinfo table. When displaying the posts, I want to display them in
>> year order and grouped by years. Is this possible using Posts::get() ?
>> If not, does anyone have any recommendations on how to do it ?

As of r3855 this can now be done with:

$posts = Posts::get(array(
'content_type' => Post::type('imprint'),
'has:info'=>'year',
'orderby' => 'info_year_value DESC',
'limit' => $limit,
'offset' => $offset,
));

Thanks for the commit, Owen.
Reply all
Reply to author
Forward
0 new messages