Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Blog Module /

Discuss the Blog Module.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Display list of blog posts, filtered by specific category


Go to End


6 Posts   4622 Views

Avatar
JamesMcMeex

Community Member, 6 Posts

23 September 2015 at 10:46pm

Edited: 23/09/2015 10:47pm

Hello, I'm hoping someone can help me, I am trying to build a section on a page to display "featured" blog posts. The desired result is that the CMS editor can get a list of blog posts on to a page, based on a category assigned to each blog post. Similar to this question from 2011 where the OP is asking about similar functionality regarding blog tags: http://www.silverstripe.org/community/forums/general-questions/show/17940

I have tried unsuccessfully to use the code suggested in the question from 2011, to see if I can use tags instead of categories. This is the function from that question:

class ArticlePage_Controller extends Page_Controller {

   public function featuredBlog($tag) {
    return DataObject::get("BlogEntry", "Tag = '{$tag}'");
  }

}

returns this error:

[23-Sep-2015 11:42:10] Error at framework/model/MySQLDatabase.php line 598: Couldn't run query: 
SELECT DISTINCT count(DISTINCT "SiteTree"."ID") AS "0"
FROM "SiteTree"
LEFT JOIN "BlogPost" ON "BlogPost"."ID" = "SiteTree"."ID"
LEFT JOIN "BlogEntry" ON "BlogEntry"."ID" = "SiteTree"."ID"
WHERE (Tag = '') AND ("SiteTree"."ClassName" IN ('BlogEntry')) 

Unknown column 'Tag' in 'where clause' (http://okoru1/projects/project-example-1/)

I am afraid I am not knowledgeable enough in PHP and MySQL to work out how blog categories and blog posts are related, in order to write the correct query function.

Any clues as to how to achieve this would be much appreciated!

Avatar
BigD1214

Community Member, 24 Posts

15 December 2015 at 11:07am

Hey any luck with this? I am having the same issue. Any help would be great! Thanks

Avatar
helenclarko

Community Member, 166 Posts

16 December 2015 at 9:06am

Edited: 16/12/2015 9:12am

Hey JamesMcMeex and BigD1214,

The link you are referencing is a little old now, it'll work but looks like it was used for another purpose.

Correct me if I am wrong, you are look to have the blog posts available on a different page?
The ability to pull blog post data from the database onto another page...

You are on the right track and may find that it will simply work by just removing the tag part.

class ArticlePage_Controller extends Page_Controller {

   public function featuredBlog {
    return DataObject::get("BlogEntry");
  }

}

But that will give you all the blog posts to deal with.
It would be best to filter the results using the page below.

https://docs.silverstripe.org/en/3.2/developer_guides/model/data_model_and_orm/#querying-data

The filtering code here is a lot newer, for example below I have included code for sorting and limiting:

get 5 blog posts sorted by date in descending order.

 class ArticlePage_Controller extends Page_Controller {

   public function featuredBlog {
    return BlogEntry::get()->sort('date', 'DESC')->limit(5);
  }

}

Then simply loop over featuredBlog in your template and you will have access to $Content, $Author, $Date, etc.

-helenclarko

Avatar
BigD1214

Community Member, 24 Posts

16 December 2015 at 9:16am

Thanks helenclarko ,
Yes but the issue i am having is filtering the blogentry by category. If i query just a blog entry i have no way of knowing what category the entry blongs to and thus cannot filter by the category. does that make sense?

Avatar
JamesMcMeex

Community Member, 6 Posts

16 December 2015 at 9:45am

Hey BigD1214, helenclarko,

Thank you both for your replies! I always worry that the Silverstripe forum is such a quiet place for such an awesome app...

I actually did find a very simple solution to this problem - apologies, I should have posted my solution (more of a workaround for my particular situation) and closed this off.

I set up a dropdown selector in the CMS that allows me to select any page from SiteTree, including blog posts:

  
<?php

class ArticlePage extends Page {
...

private static $has_one = array (
    'RelatedBlogPost' => 'SiteTree'
  );

...

  public function getCMSFields() {
    $fields = parent::getCMSFields();

...

    $fields->addFieldToTab(
      'Root.RelatedBlogPosts',
      $treedropdownfield = new TreeDropdownField(
        "RelatedBlogPostID",
        "Choose Blog Post",
        "SiteTree"
      )
    );

    return $fields;
  }

I can output data from the selected blog post into my page

It's slightly more "manual" than writing a function and looping over results, but actually my client preferred being able to "hand-pick" the blog posts they wanted on each page, regardless of the tags or categories associated with that post.

@BigD1214 this may not an appropriate workaround for you, but I hope it's helpful nonetheless!

Avatar
TGx`

Community Member, 10 Posts

27 June 2017 at 5:25pm

I am having the same issue. Any help would be great! Thanks.

__________________________________________________________
แทงบอล sbobet