Jump to content

Module: Blog


kongondo

Recommended Posts

We are talking about two different things here then. You said templates (blog-post) but it seems you mean template files (blog-post.php)? What do you mean by layout? Do you mean the HTML markup or the layout of fields in the template? If HTML, Blog will not get in your way. You can use whatever template file you like but you need to associate your blog posts with the template blog-post. I don't know how much you've read up on the separation of these two concepts but you can have your posts use the template blog-post with a totally differently named template file or none at all. In a nutshell, Blog doesn't care about template files - just the templates. Btw, a couple of posts up I have provided a Gist of the method renderPosts() whose markup you can modify to suit your needs (if you are referring to the markup renderPosts() generates) and use that instead of Blog's renderPosts(). That will not stop you from using Blog's other methods, e.g. renderTags().

Link to comment
Share on other sites

@Peter Knight I can confirm that Kongondo's example gist works no problem. Can easily add mark up to the necessary parts of the function. In my case I've added divs to suit bootstrap. No problem. And if you wanted to create separate markup for a single post you could either do it with a separate function or with a conditional statement within the renderPosts() function.

  • Like 2
Link to comment
Share on other sites

Peter, I've added a lot of custom fields to my blog-post template, so my template file for the blog post itself is going to have a lot of calls to the fields listed for the template so that I can't rely on the module to output everything I want in the way that I want. It should be totally possible, and the module has a lot of other features that make it worth my while (multiple authors, the dashboard, etc). Unless you're stripping even more off the blog than just the template output for the individual posts, I think I'd stick with using it. My 2¢!

  • Like 2
Link to comment
Share on other sites

@kongondo is the total blog-post count for the user stored anywhere?

I'm trying to list my authors (in a custom admin page) in order of post count (greatest to least). Anything you can think of that would do it? Thanks again!

I see "$authorPublishedCnt = count(wire('pages')->find("template=blog-post, created_users_id={$author->id}"));" in ProcessBlog.module line 1840, which seems about what I'm looking for... though I'd like to subtract unpublished posts (which I can do with math in a later line if necessary). Is there a way for me to access created_users_id in admin? I tried that and createdUser but they both threw errors.

I love how typing out a question to you almost always helps me think through it. Sometimes I'm lucky enough to be inspired to try something before I post the question. Other times, not so much.

So the call I ended up using was:

$points = count(wire('pages')->find("template=blog-post, created_users_id=$mobster->id"));

The trouble I was having was that I was comparing the user id to the page name. Doink!

Edited by creativejay
Link to comment
Share on other sites

Cool. But that will just show the number of posts they have but not (necessarily) sorted according to their individual posts count. For that you would need something like this...

$posts = $pages->find("template=blog-post, sort=created_users_id.count");//you might want to limit results depending on use case

//test it....it should output the list in DESC order...
foreach ($posts as $post) {

  echo $post->createdUser->title . ' - ' . $post->title . '<br>';
  
}

Count selectors – finding matches by quantity

http://processwire.com/api/selectors/#count

  • Like 1
Link to comment
Share on other sites

Hey, look at that! I stepped away before tackling the sort order and you did the work for me!

I have to add some posts for the other users to test this, since for now post count order is the same as the default. I'll report back in a few minutes. Just wanted to say my thanks!

Link to comment
Share on other sites

Okay, I see your query was looking at blog-post, so the created_users_id.count applies to the field created_users_id which is part of a blog-post page.

What I'm actually looking to do is sort the user list by the total (published) posts attributed to the user.. so I need to build that attribution somehow (before the query).

What would you do? My brain keeps telling me I should create a new field for template user called user_post_count, and write a hook for each post save that would assign the value of created_users_id.count to that field. Is that too convoluted?

Link to comment
Share on other sites

That's exactly what that code does :-). Did you try it? Or am I missing something here? What do you want your list to show? Authors only? Or authors + their count of posts? e.g. John - 35, Mary 23, Simpson 5?

Edited by kongondo
Link to comment
Share on other sites

It didn't quite work as-is. The list it output was the top user and the title of a post, one line for each post. Then it sorted the next user's single post.

So it did sort correctly, but it didn't display the number of posts for that user and I only wanted one result per user.

So I attempted to use the sort filter but of course since I was still searching users, not posts, it wouldn't work.

In the screen below you see the table with the correct information but the wrong order. Beneath that is the order and information from your code.

admin-custom-page-user-list-by-published

Link to comment
Share on other sites

I see. The following should do it. It could probably be made simpler but can't think straight this late hour :D

//find blog authors
$authors = $users->find('roles=blog-author|superuser, sort=title');
//array to hold author TITLES and their posts' count
$authorsList = array();

foreach ($authors as $author) {
    //count number of posts by this author
    $authorPublishedCnt = count($pages->find("template=blog-post, created_users_id={$author->id}"));

    //we assume no two authors with identical TITLES, otherwise older will be overwritten
    //author title is $key and their post count $value
    $authorsList[$author->title] = $authorPublishedCnt;

}

//sort the associative array $authorsList by posts count ($value), DESC
arsort($authorsList,1);

//rest of your code here to loop through the sorted array above
Link to comment
Share on other sites

You have the author title in the array keys...But you have to first make sure that you have entered the 'titles' of your authors in their user pages, i.e. the 'Display name (first and last name) field that Blog adds to user pages otherwise the array keys will be empty :-). Again, we are using their 'titles' not their 'names'. However, if you want to use their names instead, just modify the code above like so...(but I prefer title :-))

//find blog authors
$authors = $users->find('roles=blog-author|superuser, sort=title');//sort doesn't matter here, so can remove
//array to hold author NAMES and their posts' count
$authorsList = array();

foreach ($authors as $author) {
    //count number of posts by this author
    $authorPublishedCnt = count($pages->find("template=blog-post, created_users_id={$author->id}"));

    //we assume no two authors with identical NAMES, otherwise older will be overwritten
    //author NAME is $key and their post count $value
    $authorsList[$author->name] = $authorPublishedCnt;

}

//sort the associative array $authorsList by posts count ($value), DESC
arsort($authorsList,1);

//output the author list and post counts
foreach ($authorsList as $key => $value) {
    #this is just for testing. Use your own markup of course
    echo 'Author Title: ' . $key . ' - Author Post Count: ' . $value . '<br>';
}
  • Like 2
Link to comment
Share on other sites

Thank you kongondo! As usual I can go to sleep trusting that the ProcessBlog Fairy will leave working PHP code underneath my pillow...

This seems to be the line that made the difference "foreach ($authorsList as $key => $value) {"

Got that working now! I'm sure I'll be back with something else soon! ;) Thanks again!

Link to comment
Share on other sites

Great Module! Thanks!

I miss one litte functionality - display related posts (or did I miss something?).

Maybe someone else needs this, so here is the code (I use this in a TemplateDataProvider):

  public function populate() {
    $limit = 3;
    $selector = array(
      'template' => 'template=blog-post',
      'id' => 'id!=' . $this->post->id,
      'categories' => 'blog_categories=' . $this->post->blog_categories,
      'tags' => 'blog_tags=' . $this->post->blog_tags
    );

    // find posts same tag AND category
    $related = wire('pages')->find(implode(', ', $selector));

    if ($related->getTotal() < $limit) {
      // find posts same category
      $related = $this->getRelated($related, $selector, 'tags');

      if ($related->getTotal() < $limit) {
        // find posts same tag
        $related = $this->getRelated($related, $selector, 'categories');
      }
    }

    $this->related = $related;
  }

  private function getRelated($related, $selector, $remove) {
    $selector['id'] .= '|' . (string)$related;
    unset($selector[$remove]);
    $rel = wire('pages')->find(implode(', ', $selector));

    return $related->import($rel);
  }

  • Like 5
Link to comment
Share on other sites

Regarding "related posts", I also think that it's a pretty important for any blog out there. As a blogger you want readers to stay as long as possible and visit as many posts as possible. A feed of related posts is a great way to achieve that.

Not sure if it's still up-to-date, but I've used this Gist by Soma in some projects a while ago, mainly for the scoring part :)

  • Like 2
Link to comment
Share on other sites

I just noticed that a superuser who created 9 pages that are unpublished OUTSIDE of the blog in the tree has a listing of 9 pages as pending in the Blog / Author dashboard. Not sure why, as the module seems to limit it to the blog-post template. I thought I'd mention it.

Well that's just plain impossible unless you changed something :D. Just to be sure, I've just tested it...

  • Like 1
Link to comment
Share on other sites

Regarding "related posts", I also think that it's a pretty important for any blog out there. As a blogger you want readers to stay as long as possible and visit as many posts as possible. A feed of related posts is a great way to achieve that.

Not sure if it's still up-to-date, but I've used this Gist by Soma in some projects a while ago, mainly for the scoring part :)

Thanks for reminding me about this one (I actually forked it a long time ago and forgot about it!! Never thought to use it). Interesting approach Soma uses  - raw MySQL (although I don't think I'll have to hook into anything if I adopt this). I'll compare it more closely to justb3a's plus any other ideas I might have and make a decision...

Link to comment
Share on other sites

Also wondering whether to define a 'related post' as only one that has this matching 'tag' AND this matching 'category' to the exclusion of also has either this matching 'tag' OR has this matching 'category'...Hmm, maybe let the dev decide...

Edit:

Also wondering if can/should use or-groups here? Just my loud musings here in case I forget...ignore me :-)

Edited by kongondo
Link to comment
Share on other sites

Well that's just plain impossible unless you changed something :D. Just to be sure, I've just tested it...

I agree, I'm looking at line 1841 in my (untouched) ProcessBlog.module. I see no reason that pages with template 'form-builder', 'blog', 'blog-widgets', 'swmb_error', or 'admin' should show up in that list. However, when I add 'parent=/blog-posts/' to the filter, the pending count returns to 0.

I'm just the black child fluke-finder. ;)

Link to comment
Share on other sites

I'm searching back through this discussion and not finding if anyone ever came up with a solution that prevented blog-author from editing other users' posts (other than their own).

Was anything ever worked up or around to limit people to editing just their own content?

EDITED TO ADD: Okay, I found this which blocks the users from editing other people's content:

https://processwire.com/talk/topic/3875-module-to-add-userid-to-pages-and-control-edit-permissions/?p=37915

I'm getting there!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...