Jump to content

Find by date


joshuag
 Share

Recommended Posts

hi all,

I have been creating a blog in PW, normal. I have done this a few times. However, this time I decided that I would like to organize my posts in parent categories to make it easier to see in the admin what posts are in what main category... so I made a page structure like so:

/blog/ (template=blog)

- /design/ (template=category) (33)

- /general/ (template=category) (33)

- /music/ (template=category) (34)

- /Example-post/ (template=blog-post)

Inside those "category" pages I imported my old blog posts and sorted them into each category.

So let's pretend that I have 100 posts and I have 33, 33, 34 posts in each category.

So when I created my template for blog, I wanted to get the most recent posts. I have a date/time field called "published_date" - all the blog-posts have a publish_date value (dates spanning a couple years) and I have autojoined the field to my template.

Anyhow, I did a selector like so:

$posts = $pages->get('/blog/')->find('template=blog-post,sort=-publish_date, limit=10');

But this doesn't return the most recent posts across all categories, it returns me the most recent posts in the first category... well, the first 10 blog-posts in "design" category and they are sorted by publish_date.

Is it possible to find by date first? So that I can find all the blog-post items, then sort them by date... also, I know that I could find them all, but I need to limit my results and paginate.

I am afraid that I made a big oversite when doing this way and just might have to put all my posts into one parent page and select categories with a page field instead.

Thanks in advance for any help...

Link to comment
Share on other sites

Your example looks okay to me, and it should work. Try these to see if any fix the problem:

1. Try dragging your /blog/ to be a child of some other page, and then drag it back out to root. That should force a rebuild of the pages_parents index, used by the particular find() function you are calling. If there was a problem in the index, this would resolve it.

2. Try adding "include=all" to your selector, just in case the pages that aren't showing up are unpublished or hidden. Though for your production version, you probably don't want "include=all", but it's a good test for now.

3. Rather than $pages->get("/blog/")->find(...), try it without the get("/blog/'). That part is adding a little overhead, so if you aren't going to find pages using template blog-post outside of /blog/, then there's no reason to have that /blog/ filter there. So try changing it to this:

$pages->find("template=blog-post, sort=-publish_date, limit=10"); 

4. Are you certain that template blog-post has a field called publish_date and that it is a date field (not a text field)? And do all the pages in question have this field populated?

Let me know if none of these fix it, or if #3 fixes it. If #3 works while the alternative doesn't, then I'd want to do a little testing here to make sure it's not a bug in PW's Page::find() function.

Link to comment
Share on other sites

Awesome! Yeah, looks like my index was out of shape. Moving the blog and putting it back fixed it. I am really glad that this works. I was thinking that I had been mistaken about how processwire selects from the DB. This is fantastic news :)

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...