Searching by tags with limit and offset

j9cj9c
edited December 2015 in Modules
I have a News module which uses Fuel's Tags module. I am trying to retrieve a limited amount of record with an offset from the News module of a certain tag for pagination. I also would like it to be ordered by date in a descending order. I've tried searching by tags using fuel_model first but I failed to realize that the WHERE clause is for searching tags. It would be great help if anyone could provide a solution.

Comments

  • edited December 2015
    To grab the news items from a certain tag, you can try the following:
    $slug = uri_segment(3); // or whatever URI segment the tag slug is $tag = $CI->fuel->tags->find_by_tag($slug); $news_model = $tag->get_news(TRUE); $limit = 10; $offset = 10; $items = $news_model->find_all(array(), 'publish_date desc', $limit, $offset);
    Note the $news_model = $tag->get_news(TRUE);. The "get_news" method maps to essentially $tag->news. However, passing TRUE to it will return a reference to the news model with the "where_in" active record already applied to it so you can do further manipulation such as setting the ordering, offset and limit.

    Also, FUEL has some built in capabilities to generate those module tag pages for you.
    http://docs.getfuelcms.com/modules/simple#post_pages

    We have a demo site that you can download from GitHub that helps demonstrate this and includes both a news and an events type module:
    https://github.com/daylightstudio/daylight-roasters
  • j9cj9c
    edited 5:02AM
    Thank you very much! :)
  • j9cj9c
    edited 5:02AM
    I get this error when I use that code

    Call to a member function get_news() on a non-object
  • edited 5:02AM
    There was an error in the code above in which it didn't grab the actual $tag. I've updated above.
Sign In or Register to comment.